[RESOLVED] Displaying All Records
My code is only showing one record from the database how would i get it to show all the records from the database and not just one.?
PHP Code:
<?php
include("config.php");
mysql_connect("$host", "$username", "$password");
mysql_select_db($db) or die(mysql_error());
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM ip")
or die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo "IP: ".$row['ip'];
echo " Reason: ".$row['Reason'];
?>
Re: Displaying All Records
PHP Code:
while($row = mysql_fetch_array( $result )) {
echo "IP: ".$row['ip'];
echo " Reason: ".$row['Reason']."<br />";
}
You need to loop through all the returned rows of the query.
Re: Displaying All Records
What would i need to do if there is no records how to i get it to display a message?
EDITED:::- I just added a new IP address and now it dosent show them on the display page but when i add another one it then displays the second ip address and not the first one and displays all the reson from there on.
http://prksoftware.co.uk/display.php
Re: Displaying All Records
Can you post all your code?
If there are no IPs, you can use the mysql_num_rows to check if there is at least one returned from your SQL statement or not.
Re: Displaying All Records
Config.php
PHP Code:
<?
// Please edit the variables below:
//Set the MySQL server address (usually localhost)
$host = "localhost";
//Enter the login name you wish to access your database with
$username = "web181-ipbanner";
//Enter the password to access the database
$password = "d82f1fc";
//Enter the database you wish to use
$db = "web181-ipbanner";
?>
display.php
PHP Code:
<?php
include("config.php");
mysql_connect("$host", "$username", "$password");
mysql_select_db($db) or die(mysql_error());
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM ip")
or die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
while($row = mysql_fetch_array( $result )) {
echo "<br>IP:<br> ".$row['ip'];
echo "<br>Reason:<br> ".$row['Reason']."<br /><hr>";
}
?>
Re: Displaying All Records
PHP Code:
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
the problem is with this line. You fetch the first entry from the database and do nothing with it. It's the same command that's in the while condition so you don't need it separately above it. That's why the first entry doesn't show up. It gets loaded in $row, and then when the loop starts gets overwritten with the 2nd entry.
Re: Displaying All Records
shall i remove this code.
PHP Code:
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry