Connecting PHP with MySQL

Connecting to MySQL with PHP is very simple. Following is a sample table and script:

+----+--------+-------+
| id | make   | model |
+----+--------+-------+
|  1 | toyota | camry |
|  2 | toyota | rav4  |
|  3 | honda  | crv   |
+----+--------+-------+

  1 <?php
  2   $host = 'localhost';
  3   $user = 'titanic1912';
  4   $pass = 'lusitania1916';
  5   $db = 'cars';
  6   $conn = mysql_connect($host, $user, $pass) or die ('Could not connect to db');
  7   mysql_select_db($db) or die ('could not select db');
  8   $q = 'select * from cars';
  9   $rs = mysql_query($q);
 10   while($rw = mysql_fetch_object($rs))
 11   {
 12     print $rw->toyota . ' ' . $rw->model . '
'; 13 } 14 mysql_close($conn); 15 ?>

Line 6 connects to the mysql database. In line 7, we select our desired database. Line 9 runs the query. Line 10 fetches the results from the query and line 12 prints the results. Line 14 closes the connection.

If you are a beginner, substitute your own values from line 2 to 5. Then modify the query in line 8. Finally edit line 12 to perform your desired task.