Using PHP to Connect to a MySQL server

Posted by | Posted in Code, MySQL, PHP | Posted on 18-07-2009

This is how I normally link PHP to my MySQL servers. I use this code normally for my shell scripting, but it also works for websites.

$mysql_server = "SERVER_ADDRESS";
$mysql_user = "SERVER_USER";
$mysql_password = "SERVER_PASSWORD";
$mysql_database = 'SERVER_DATABASE';
$mysql_table = 'SERVER_TABLE';
$mysql_connection = @mysql_connect($mysql_server,$mysql_user,$mysql_password);
if (!$mysql_connection)
  DIE ('ERROR 10: No Connection to '.$mysql_server.' '. mysql_error() ."\n");
$mysql_database_connection = @mysql_select_db($mysql_database,$mysql_connection);
if (!$mysql_database_connection)
  DIE ('ERROR 30 : Could not select database '. $mysql_database .' '. mysql_error() ."\n");
echo "MySQL : Connection Active \n";

Comments are closed.

Join the mailing list

Check your email and confirm the subscription