Learn - Develop - Deploy - Enjoy

Howto: Connect to a MySQL Database with PHP plus more

September 23rd, 2008 Sam

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.

Howto: PHP Hello World Script

February 15th, 2008 Sam

One of the First things you will ever do with PHP is create a Hello World Script.

Script 1

<?php

Print “Hello, World!”;

?>

Script 2

<?php

Echo “Hello, World!”;

?>

Script 1 & 2 do exactly the same thing the out the words Hello World!

Script 1 uses Print to Display Hello World!

Script 2 uses Echo to Display Hello World!