MySQL

  • Have to compile PHP with MySQL support (for PHP < 4).
  • Use the --with-mysql[=DIR] configuration option to enable PHP to access MySQL databases.

Functions

  • $array = mysql_fetch_array( $result_set [, RESULT_TYPE = MYSQL_BOTH] )
RESULT_TYPE:
MYSQL_ASSOC Fieldname index
MYSQL_NUM Numerical index (zero-based)
MYSQL_BOTH Numerical and fieldname index

Examples

<?php
// Connect to the MySQL database.
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect");
mysql_select_db("my_database") or die("Could not select database");

// Get all records in the table.
$query = "select * from my_table";
$result_set = mysql_query($query) or die("Query failed");

// Get the field values for the current row.
while ($row = mysql_fetch_array($result_set, MYSQL_ASSOC)) {

// Get the field values for the current row.
foreach ($row as $col_value) {
print "$col_value, ";
}
print "\n";
}
mysql_free_result($result_set);

// Disconnect from the MySQL database.
mysql_close($link);
?>

Resources URL: 
notes/php/resources
Sources URL: 
notes/php/sources

See Also