|
Displaying Data
Introduction So far in this tutorial, you have created a database and put information into it. In this part I will show you how to create an input page for your database, and how to display the whole contents. Inputing the data using HTML pages is almost identical to inserting it using a PHP script. The benefit, though, is that you do not need to change the script for each piece of data you want to input and you can also allow your users to input their own data. The following code will show an HTML page with textboxes to enter the appropriate details: $first=$_POST[’first’]; mysql_connect(localhost,$username,$password); $query = “INSERT INTO contacts VALUES (”,’$first’,'$last’,'$phone’,'$mobile’,'$fax’,'$email’,'$web’)”; mysql_close(); You could also add to this script a message confirming the data input. This is basic PHP, though, and you should read the PHP tutorial if you do not know how to do this. Outputting Data Now you have at least one record, if not many more, in your database you will be wanting to know how you can output this data using PHP. Before beginning, though you should be familiar with loops in PHP (you can find out about them in the tutorial on Free Webmaster Help) as they are used for this way of outputting data. The first command you will need to use is a MySQL query made up like this: Counting Rows Before you can go through the data in your result variable, you must know how many database rows there are. You could, of course, just type this into your code but it is not a very good solution as the whole script would need to be changed every time a new row was added. Instead you can use the command: $num=mysql_numrows($result); This will set the value of $num to be the number of rows stored in $result (the output you got from the database). This can then be used in a loop to get all the data and output it on the screen. Setting Up The Loop nYou must now set up a loop to take each row of the result and print out the data held there. By using $num, which you created above, you can loop through all the rows quite easily. In the code below, $i is the number of times the loop has run and is used to make sure the loop stops at the end of the results so there are no errors. CODE $i++; Assigning The Data To Variables The final part of this output script is to assign each piece of data to its own variable. The following code is used to do this: Combining The Script We can now write a full script to output the data. In this script the data is not formatted when it is output: mysql_connect(localhost,$username,$password); $num=mysql_numrows($result); mysql_close(); echo “<b><center>Database Output</center></b><br><br>”; $i=0; $first=mysql_result($result,$i,”first”); echo “<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>”; $i++; ?>
Comments
|
Recent Posts
No Title
Library Films EbnEzz.net Part 3 - Password Protection Part 2 - .htaccess Commands Part 1 - Introduction Displaying Data EbnEzz Submit Tutorials Syndication Tools |
|
You are not logged in. FREE Sign Up or Log In
©2009 Flixya Entertainment, LLC. All rights reserved.




Free Sign Up - Start Making Money on Flixya »



