Hide options

Connect to a MYSQL database with PHP

Simple example how to use MYSQL in PHP scripts
<?php
   /*
    * Using a mysql database with PHP
   */
  
   // Setup the connection to the database
   $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect"); 

   // Select the database to query
   mysql_select_db("my_database") or die("Could not select database"); 

   // Define the SQL query
   $query = "SELECT field1, field2 FROM my_table"; 
   
   $result = mysql_query($query) or die("Query failed"); 

   // Present the result in HTML 
   print "<table>"; 
   while ($row = mysql_fetch_array($result)) { 
      print "<tr>"; 
      print "<td>" . $row['field1'] . "</td>";
      print "<td>" . $row['field2'] . "</td>";
      print "</tr>"; 
    } 
    print "</table>"; 

    // Free resultset 
    mysql_free_result($result); 

    // Close the connection 
    mysql_close($link); 
?>


Description

Example of PHP connecting to a MYSQL database. The result of the query is converted to a HTML table


Top