Blank Database Results
Posted by: Lost_Girl (---.nsw01.dataco.com.au)
Date: May 28, 2006 03:23AM

I am using the following php code shown below, and when i run it under localhost on my computer it doesnt show the results from the database it just gives me blank results even though i do have data in the database, do anyone know how to fix this or can help me with this problem.

<?php
$conn = mysql_connect("localhost","root",""winking smiley or die ("Could not connect to the server"winking smiley;
$db = mysql_select_db("produce", $conn) or die ("Could not connect to the database"winking smiley;

$query = "SELECT * FROM fruit";
$result = mysql_query($query) or die ("Query failed: " .mysql_error());

echo "<table>";
echo "<tr>";
echo "<th> Name </th>";
echo "<th> Number </th>";
echo "</tr>";

while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>", $row['$_POST = name'], "</td>";
echo "<td>", $row['$_POST = number'], "</td>";
echo "</tr>";
}

echo "</table>";

mysql_close($conn);
?>



Angela

Options: ReplyQuote
Re: Blank Database Results
Posted by: CyberSpatium (67.170.181.---)
Date: May 28, 2006 05:05AM

change these two lines:
echo "<td>", $row['$_POST = name'], "</td>";
echo "<td>", $row['$_POST = number'], "</td>";


to
echo "<td>", $row['name'], "</td>";
echo "<td>", $row['number'], "</td>";


only use $_POST when dealing with submitted form data.

I highly recommend you turn on all error reporting in your php.ini file. the reason you did not get see any php errors is because php comes with error reporting set to only display the most serious errors only, not less serious errors like your coding errors you had here. They do that for security reasons because showing all errors can be a security risk on a live server. since wamp is designed for local testing and development only, it is fine and recommended to turn all reporting on. To do so, you will need to edit your php.ini file, and find this line:

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

change it to:

error_reporting = E_ALL

Now save the file, and restart apache for the new settings to take effect.

Also, the way you are coding, especially echoing every html code line, is a very inefficient way to code your php scripts. I have edited your code a bit to clean it up and make it 100000 times easy to edit, test, and use.

=================================

<?php

$conn = mysql_connect("localhost","root",""winking smiley or die ("Could not connect to the server"winking smiley;
$db = mysql_select_db("produce", $conn) or die ("Could not connect to the database"winking smiley;

$query = "SELECT * FROM fruit";
$result = mysql_query($query) or die ("Query failed: " .mysql_error());

?>

<table>
<tr>
<th> Name </th>
<th> Number </th>
</tr>
<tr>

<?php

while ($row = mysql_fetch_array($result))
{
echo "<td>", $row['name'], "</td>";
echo "<td>", $row['number'], "</td>";
}

?>

</tr>
</table>

<?php

mysql_close($conn);

?>


=================================

Options: ReplyQuote


Sorry, only registered users may post in this forum.