Introduction
In the past two parts of this tutorial I have shown you how to take data out of the database and display it on screen. In this part I will enter into the final aspect of this data displaying, selecting one piece of data and stopping errors from happening when you output data.
Error Trapping
By outputting all the information from the database, it is quite unlikely that there will be no data, but if you allow updating and deleting of records, it is certainly a possibility. Luckily, with PHP and MySQL, there is an easy way round this using:
$num=mysql_numrows($result);
where $result contains the result of a query on the database (like selecting all the records). As I expalined before, this will set the value of $num as the number of rows in the result (and it was used in a loop in part 4). Because of this you can make a simple error trap using an IF statement:
if ($num==0) {
echo "The database contains no contacts yet";
} else {
Output Loop
}
You can expand on this more by making it more user friendly (for example by providing a link to the Add Data page if no contacts exist).
Ordering Data
Not only can you output data based on the contents of a field, but you can also order the output based on a field (for example placing users in alphabetical order). By default, the output from your queries will be in order of the id field, going from 1 upwards. You can sort it on any field, though.
For example, a useful sort would be to place all the users in alphabetical order based on their last name. For those not familiar with standard databases, this would be in Ascending order as it goes from A to Z. (Ascending order is also for 1-10 etc. and descending order provides Z to A and 10-1). To do this you would use the following query:
SELECT * FROM contacts ORDER BY last ASC
You could also use DESC to order the data in Descending order.
More Uses Of mysql_numrows and Sorting
The value you have assigned to $num is very imiportant as, apart from error trapping and loops, it has many other uses. An example of this would be to print out only the last 5 records added to a database. Firstly, they would need to be placed into order based on the id field (as the one with the latest ID would have been added last. This would require them to be in Descending order.
Now you have them in order of newest to oldest but this does not restrict the script to only showing the first 5. To do this, you would need to set your loop to run to 5 instead of $num (as this would only run the loop 5 times so only 5 records would be output).
Of course, before doing this, it would be important to check that $num was greater than 5, as if you ran the loop 5 times and there were only 3 rows you would get an error. This is easy to do though and the following code is an example of the sort of thing you would want to have:
if ($num>5) {
$to=5;
}else{
$to=$num;
}
$i=0;
while ($i < $to) {
REST OF CODE
This code would check if there were more than 5 rows in the database. If there were, the loop would be set to run 5 times. If there were less than 5 rows the loop would run the correct number of times to output the whole database.
The ID Field
If you remember back to creating the database for the contacts at the beginning of this tutorial, you will remember that we included a numerical field called id. This field was set as auto_increment as well as being the primary field. I have already explained how this field is unique for every single record in the database, but I will now take this a stage further by explaining how this can be used to select an individual record from a database.
Selecting A Single Record
At the end of the last part of this tutorial, I s
howed you how to select records from the database based on the contents of partiular fields using:
SELECT * FROM contacts WHERE field='value'
Now, by using the unique ID field we can select any record from our database using:
SELECT * FROM contacts WHERE id='$id'
Where $id is a variable holding a number of a record. This may seem to be a little worthless as it is, but you can use this very effectively in a number of different ways. For example, if you wanted to have a dynamically generated site run through a database and a single PHP script, you could write the script to include the database data into the design. Then, using the id field, you could select each individual page and put it into the output. You can even use the page's URL to specify the record you want e.g.
http://www.yoursite.com/news/items.php?item=7393
And then have the PHP script look up the record with the id corresponding to $item, which in this case would be 7393
Links For Single Records
Using this method of choosing a record using the URL to select the record can be expanded further by generating the URL dynamically. This sounds a bit complicated so I will elaborate. In the contacts script we are writing, I will be showing you how to create an Update page where the user can update the contact details.
To do this, another column will be included in the output column, with an Update link in it. This update link will point to a page allowing the user to update the record. To select the record in this page, we will put:
?id=$id
By getting the id of the record along with the other information when we are outputting the information from the database, this code will create a link which has each record's ID number in it. Then, on the update page, there can be code to just select this item
Rabu, 28 Januari 2009
Langganan:
Postingan (Atom)