PHP - MySQL connection

Sample code for connecting to a MySQL database using PHP and simple queries.

The USERNAME, PASSWORD and DATABASE_NAME variables must be substituted with those supplied for your web hosting account.

// make our connection
$connection = mysql_connect('localhost', dbname=DATABASE_NAME user=USERNAME password=PASSWORD");

// let me know if the connection fails
if (!$connection) {
    print("Connection Failed.");

// declare my query and execute
$myresult = mysql_query($connection, "SELECT somevalue FROM sometable WHERE something = 1");

// process results
$myvalue = mysql_fetch_row($myresult);

// print results
print("My Value: $myvalue<br />\n");

Wiki: support/MySQL/Making_a_connection_from_your_code/PHP_MySQL (last edited 2009-04-05 15:50:18 by BarneyDesmond)