Promotion Screen 1
Promotion Screen 2
Promotion Screen 3
Follow Us OnFacebookTwitter

Getting started with PHP

Posted on February 7th, 2010 by Craig | Linked in Hints, Tips & Tricks

So your just starting out using php and would like to see some sample php code to connect to a mysql database and retrieve some data? Sure. Let’s jump right in:

<?php

We start with the opening php tag, which tells php it should pay attention to the text after this tag as it is code.

$mysqlHostname = ‘localhost’;
$mysqlUsername = ‘myusername’;
$mysqlPassword = ‘mysqlpassword’;
$mysqlDatabase = ‘mysqldatabase’;

Here we have just setup some variables and assigned the mysql connection information to them. make sure you replace those details with your own.

if (!$link = mysql_connect($mysqlHostname,$mysqlUsername,$mysqlPassword)) {
die(mysql_errno().” : “.mysql_error());
}

Here we have established our connection to the mysql server. We have assigned the mysql connection object to $link and used the mysql connection details from earlier to connect. Should the connection fail, then the program will stop running and display the exact error for you. The ! at the front of the $link basically means equals false.

if (!mysql_select_db($mysqlDatabase)) {
die(mysql_errno().” : “.mysql_error());
}

Here we are trying to select out mysql database to work with. Should this fail, notice the ! again – then we make the program stop and print out the error details again.

$query = “SELECT id, firstname, lastname
FROM users”;
if (!$results = mysql_query($query)) {
die(mysql_errno().” : “.mysql_error());
}

Here we have created our sql query, in this case we are selecting the id, firstname and lastname colums from any entry in the users table of our database. We then run this query, by passing it to mysql_query and the results object is in $results. If it fails, ! again then the program stops running and prints out the error.

while ($row = mysql_fetch_assoc($results)) {
print “Firstname: “.$row['firstname'].”<BR>\r\n”;
print “Lastname: “.$row['lastname'].”<BR>\r\n”;
}

mysql_close($link);
?>

And finally we now get any results from the database and print them. The while loop will execute the command $row = mysql_fetch_assoc until it has no more results to get – then it will return false and cause the while loop to end. Then we close the mysql connection and put the end ?> in place.

That’s it – you have just created your php program to get results from a database connection and print them.

Here is the total program now for you:

<?php

$mysqlHostname = ‘localhost’;
$mysqlUsername = ‘myusername’;
$mysqlPassword = ‘mysqlpassword’;
$mysqlDatabase = ‘mysqldatabase’;

if (!$link = mysql_connect($mysqlHostname,$mysqlUsername,$mysqlPassword)) {
die(mysql_errno().” : “.mysql_error());
}

if (!mysql_select_db($mysqlDatabase)) {
die(mysql_errno().” : “.mysql_error());
}

$query = “SELECT id, firstname, lastname
FROM users”;

if (!$results = mysql_query($query)) {
die(mysql_errno().” : “.mysql_error());
}

while ($row = mysql_fetch_assoc($results)) {
print “Firstname: “.$row['firstname'].”<BR>\r\n”;
print “Lastname: “.$row['lastname'].”<BR>\r\n”;
}

mysql_close($link);
?>

Happy coding!

Seperator


Provider Logos

About Us | Terms and Conditions | Acceptable Use Policy | Service Level Agreement | Privacy Policy | Contact Us

© 2010 VentraIP Group (Australia) Pty Ltd