Post 01: The relationship between PHP and MySQL

PHP CAKE PHP CODE IGNITER LARAVEL

Posted on 2016-08-17 22:07:23


PHP is a programming language used to build applications Website, or in other words it is the main language used for programming the server to handle the requirements of the client. And  MySQL is a database management system for storing data and it is often used together with PHP since he was born, this is a perfect couple used to build websites.

Speaking as the perfect couple, but perhaps you do not know yet operational principles between us, right? To better understand this issue, we learn together offline.

1. Each contact between PHP and MySQL

Usually when you learn MySQL will learn T-SQL query as select, insert, update, delete. And I guess you'll be wondering if combined with PHP, then the query will be used to do this?

We see PHP as a normal human being, which means it can write out the query and execute the query it like you do. But the difference here is that you will catch PHP implementation of the query itself is not thought out.

Example : I want to get a list of all students in the school will write the query as follows.

Select * from students;

The results would execute the queries in PHP MY ADMIN is a list of students.  So the question is if in PHP must do?

As we know MySQL is a database management system, it is safe for security, meaning that it functions User management, User authorization. So before entering PHP MY ADMIN is you must sign it.

So with PHP, there must be logged steps, then will be the definition of queries and finally execute and get results.

// STEP 1: CONNECT

$conn = mysqli_connect('localhost', 'USER', 'PASS', 'DATABASE')
or die ('Can't connect to database');
 
// STEP 2: CREATE QUERY
$sql = 'SELECT * FROM students';
 
// STEP 3: REQUEST EXECUTE QUERY
$result = mysqli_query($conn, $sql);
 
// STEP 4: HANDLE DATA RETURN
while ($row = mysqli_fetch_assoc($result)){
    var_dump($row);
}

In summary PHP will be hooked for connection requests MySQL MySQL and execute the query and returns the result to PHP PHP to take the results from there to handle.

2. Conclusion

Not only in PHP MySQL which can combine with other database management systems such as SQL Server, SQL Lite, MongoDB, Cassandra. The problem is that the project should use any database management system for each management system has its advantages and disadvantages.