Archive - October, 2009

Querying A mySQL Database Using PHP

Querying a mySQL database requires an established connection to a mySQL database.  You can read our article on connecting to a mySQL database here.

Querying the mySQL database is as simple as creating the SQL query and passing it through the mysql_query function.

<?
$sql = "SELECT * FROM products";
$results = mysql_query( $sql, $resource );
?>

With the $results variable you can use it to do a few different things. The first one we will discuss is to determine the total number of rows that was returned by the Select query. You can do that by doing the following:

<?
$num_rows = mysql_num_rows( $results );
?>

It should be noted that using mysql_num_rows is not the most efficent way to determine the number of rows being returned. If there are a lot of rows (I have seen it happen with as few as 1000 rows) that it is faster to query the database for the number of results like this:

<?
$sql = "SELECT COUNT(*) as num_rows FROM products";
$num_rows = mysql_fetch_array( mysql_query( $sql ) );
?>

This example also introduces the mysql_fetch_array function. This function is used to grab the next row of a mySQL query result. The row is returned as an array with the indexes named as the names of the columns in the database. Once there are no more results the function returns null.

The best way to iterate through a result set of a mySQL query is to use a while loop:

<?
$sql = "SELECT * FROM products";
$results = mysql_query($sql);
while( $row = mysql_fetch_array($results) ) {
    echo $row['products_id'] . ': '. $row['products_name'] . '
'; } ?>

Connecting To A mySQL Database Using PHP

Connecting to a mySQL database is a very important part of any PHP application. mySQL has become an integral part of many PHP applications since it is also free to use. Connecting to a mySQL database only requires a single function call.

<?
$resource = mysql_connect( $server, $username, $password );
?>
  • $resource – The variable that the connection will be stored and passed to the other mySQL functions that are going to be called.
  • $server – The address of the database server.  Remembering that this is relative to the server, not your end machines.  For example, if your database server is located on the same machine as your webserver, this will be “localhost”.  If it is in the same datacenter (or network) it could be an ip address like 10.xxx.xxx.xxx or 192.xxx.xxx.xxx.  Otherwise you can use an entire URL like db.mydatabaseurl.com.
  • $username – Username to use to login to the database server
  • $password – Password to use to login to the database server

PHP Variable Syntax

PHP is a dynamically typed language. What this means is that you do not have to specify the variable type. You do not even need to declare you variable before using it. All variables have a $ in front of them and cannot be more than 64 characters long. String variables require that the value be surrounded by double or single quotes. Numerical values do not need to surrounded by quotes, but they can be if you so desire. They will still be recognized as numerical (PHP is nice that way).

For example, here are some basic variable declarations:

<?
$address = "123 Somewhere"; $zip = 49123
?>

PHP Hello World

Not be cliché, but I any book, tutorial or website discusses a programming language always begins with a program that simply displays the text “Hello World” on the screen and I intend to follow that tradition.

The process of writing text in PHP requires you to use either the “echo” of “print” function. There are some very minute differences between these two functions, but for these purposes, we will just say that they are interchangeable. If you really want to know the differences, there is an article that details this here.

Anyways, here is the hello world program for PHP.

<?
echo "Hello World";
?>

You could also use the print function and that would look like this:

<?
print "Hello World";
?>

You will notice that at the end of each line of code there is a semi-colon. Like many other languages, this is used at the end of most of the lines of code.

A Brand New Look, Some Changes, and An Announcement

I have made a lot of different changes to the website this weekend and I thought I should let everyone know about what is happening with the website.

First are foremost, I have changed the design of the website. I was never truly satisfied with the old look and I like this one a lot better. Your comments are welcome, but please remember that I am a programmer, not a designer (so constructive criticism only).

You will also notice that I have removed all advertising from the website. It hurt the look of the site and nobody was clicking on them anyways. There will still be advertising in the RSS feed, but that is the only place for now. No advertising now means that I can get rid of that ugly privacy policy.

I would also like to take this time to announce the launch of a new website that I will begin doing called Learn PHP Now. It will cover the exact same type of topics as this website, but with PHP. There were posts on this website where using PHP would have been very helpful to some of you, but it was not within the scope of this website to do so. So now I can fill in that gap with the launch of Learn PHP Now.

Page 2 of 4«1234»