Hey. I am new to php and am trying to learn. What I'm doing is querying(did I spell this right?) a mySQL database and putting the results into a table on a webpage. I have worked for hours on this and I finally got the answer I was looking for...almost. The problem is that the first result is not displayed.
For example, my table in the database looks like:
Item Number <-- Column Names
Him 5
Fim 20
Kim 8
Lim 12
The results that are printed in the table are:
Fim 20
Kim 8
Lim 12
The number of rows that the SQL returns is 4 so I don't understand why I cannot display all 4 rows in the table. Line 64 is where the while loop starts where I think something is wrong.
What am I doing wrong? PS. There are a lot of commented lines that I couldn't get to work but are there for my reference, I apologize for not taking them out. I may have left something out that I missed.
Edit: No errors. Just the first row in the mysql table is not showing up in the table on the webpage.
- <?php
-
$dbname = $_POST["dbname"];
-
$quantity = $_POST["quantity"];
-
$itemnumber = $_POST["itemnumber"];
-
-
include "include/z_db.php";
-
$sql = sprintf("SELECT * FROM `distributor` WHERE 1", mysql_real_escape_string($itemnumber));
-
endif;
-
?>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html><head>
-
<style type="text/css"> .tbl1 { background: url('back.jpg'); background-repeat: no-repeat;} </style>
-
<title>Search</title>
-
</head>
-
<body style="background-color: rgb(255, 255, 255);" link="#cc0000" vlink="#660033">
-
<table class="tbl1" border="0" cellpadding="0" cellspacing="0" width="647">
-
<tbody>
-
<tr>
-
<td colspan="4"><img src="single_pixel.gif" border="0" height="1" width="740"/></td>
-
</tr>
-
<tr valign="top">
-
<td height="91" width="60%"><img src="single_pixel.gif" height="200" width="1"/></td>
-
<td colspan="3" align="center" height="91" valign="top" width="40%"> </td>
-
</tr>
-
<tr>
-
<td colspan="3"> </td>
-
</tr>
-
<tr>
-
<td colspan="4" valign="top">
-
<table border="0" cellspacing="15" width="100%">
-
<tbody>
-
<tr>
-
<td valign="top" width="8%"><img src="single_pixel.gif" height="1" width="180"/><br />
-
<b>Menu</b>
-
<br />
-
<a href="userare.php">User Area</a><br/>
-
<a href="contact.html">Contact</a><br/>
-
<a href="logout.asp">Logout</a><br/>
-
</td>
-
<td width="92%">
-
<b>Search Results</b><br />
-
<?php
-
$result = mysql_query($sql) or die(mysql_error());
-
$re = mysql_fetch_array($result);
-
-
-
$num = mysql_num_rows($result);
-
echo "$num rows<br />";
-
if (mysql_num_rows($result) < 1):
-
$error[] = "The search term provided {$itemnumber} yielded no results.";
-
else:
-
$results = array(); // the result array
-
$i = 1;
-
while($row = mysql_fetch_array($result)):
-
-
//echo $row['itemnumber']. " = " .$row['quantity']. "<br>";
-
-
// echo "{$i}";
-
//echo "Column 1 is {$row['itemnumber']} {$row['quantity']}<br>\n";
-
-
//endif; if change while to if uncomment this line
-
$results[] = "<tr><td>{$row['itemnumber']} </td><td>{$row['quantity']}</td></tr>";
-
$i++;
-
-
//$row = mysql_fetch_array( $result , MYSQL_ASSOC )
-
endwhile;
-
echo "<center><table border=1 bgcolor=><tr><td>Item Number</td><td>Quantity</td></tr>";
-
foreach($results as $v){
-
echo "{$v}";
-
}
-
echo "</table></center>";
-
endif;
-
?>
-
-
</td>
-
</tr>
-
</tbody> </table> <h2> </h2> </td> </tr> </tbody> </table></body>
-
</html>