Howto: Connect to a MySQL Database with PHP plus more
So how is everyone doing?
Well today I am going to teach you how to connect to a mysql database, query a mysql database and then get back some data.
You only need one Connection.
//Start Connection
$connection = mysql_connect(’host’,'username’,'password’);//Start Connection 2 with an if statement incase the connection fails
$connection = mysql_connect(’host’,'username’,'password’);
if (!$connection) {
die(’Could not connect: ‘ . mysql_error());
}
//Select Database
mysql_select_db($dbname);//Query
$query = “SELECT * FROM Table_Name”;
//Execute the Query
$result = mysql_query($query);//Display the Data
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo “Name :{$row['name']}” .
“Subject : {$row['subject']} ” .
“Message : {$row['message']}”;
}
//Clear Results
mysql_free_result($result);
//Close Connection
mysql_close($connection);
Here are a few more MySQL functions that can help you.
//List MySQL Databases
mysql_list_dbs($connection);
//List MySQL Tables in a Database
mysql-list-tables(”Database_Name”);
//Number of Rows returned by results from a query
mysql_num_rows($result);
//MySQL Host Info
mysql_get_host_info($connection);
//MySQL Server Info
mysql_get_server_info($connection);
If you have any questions simply leave a comment with your questions.
Hey if its a question about code remember to post the code.
























Leave a Reply