Connecting Tech Pros Worldwide Forums | Help | Site Map

When printing SQL query, only the first result is not displayed

Newbie
 
Join Date: Oct 2009
Location: Roll Tide
Posts: 2
#1: 4 Weeks Ago
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbname = $_POST["dbname"];
  3. $quantity = $_POST["quantity"];
  4. $itemnumber = $_POST["itemnumber"];
  5.  
  6. include "include/z_db.php";
  7. $sql = sprintf("SELECT * FROM `distributor` WHERE 1", mysql_real_escape_string($itemnumber));
  8. endif;
  9. ?>
  10.  
  11. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  12. <html><head>
  13.     <style type="text/css"> .tbl1 { background: url('back.jpg'); background-repeat: no-repeat;} </style>
  14. <title>Search</title>
  15. </head> 
  16. <body style="background-color: rgb(255, 255, 255);" link="#cc0000" vlink="#660033">
  17.     <table class="tbl1" border="0" cellpadding="0" cellspacing="0" width="647">
  18.         <tbody>
  19.             <tr>
  20.                 <td colspan="4"><img src="single_pixel.gif" border="0" height="1" width="740"/></td>
  21.             </tr>
  22.             <tr valign="top">
  23.                 <td height="91" width="60%"><img src="single_pixel.gif" height="200" width="1"/></td>
  24.                 <td colspan="3" align="center" height="91" valign="top" width="40%"> </td>
  25.             </tr>
  26.             <tr>
  27.                 <td colspan="3"> </td>
  28.             </tr>
  29.             <tr>
  30.                 <td colspan="4" valign="top">
  31.         <table border="0" cellspacing="15" width="100%">
  32.             <tbody>
  33.                 <tr>
  34.                     <td valign="top" width="8%"><img src="single_pixel.gif" height="1" width="180"/><br />
  35.                         <b>Menu</b>
  36.                         <br />
  37.                         <a href="userare.php">User Area</a><br/>
  38.                         <a href="contact.html">Contact</a><br/>
  39.                         <a href="logout.asp">Logout</a><br/>
  40.                     </td>
  41.                     <td width="92%">
  42.                         <b>Search Results</b><br />
  43.                         <?php
  44. $result = mysql_query($sql) or die(mysql_error());  
  45. $re = mysql_fetch_array($result);
  46.  
  47.  
  48. $num = mysql_num_rows($result);
  49. echo "$num rows<br />";
  50. if (mysql_num_rows($result) < 1):
  51.     $error[] = "The search term provided {$itemnumber} yielded no results.";
  52. else:
  53.     $results = array(); // the result array
  54.     $i = 1;
  55.     while($row = mysql_fetch_array($result)):
  56.  
  57. //echo $row['itemnumber']. " = " .$row['quantity']. "<br>";
  58.  
  59. // echo "{$i}";
  60. //echo "Column 1 is {$row['itemnumber']} {$row['quantity']}<br>\n";
  61.  
  62. //endif; if change while to if uncomment this line
  63.         $results[] = "<tr><td>{$row['itemnumber']} </td><td>{$row['quantity']}</td></tr>";
  64.         $i++;
  65.  
  66. //$row = mysql_fetch_array( $result , MYSQL_ASSOC )
  67.     endwhile;
  68. echo "<center><table border=1 bgcolor=><tr><td>Item Number</td><td>Quantity</td></tr>";
  69. foreach($results as $v){
  70.         echo "{$v}";
  71. }
  72. echo "</table></center>";
  73. endif;   
  74.                         ?>
  75.  
  76.                     </td>
  77.                 </tr>
  78.             </tbody>        </table>                        <h2>&nbsp;</h2>                    </td>                </tr>        </tbody>    </table></body>
  79. </html>
best answer - posted by Atli
Hey.

Line #45.
You fetch the first row into the $re variable on that line, so when you start your loop on line #55, the first row in the result is already gone.

If you comment line #45 out this particular problem should be fixed.

Although, on line #8 you have a random "endif;" which doesn't seem to belong anywhere. That should cause a parse error.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#2: 4 Weeks Ago

re: When printing SQL query, only the first result is not displayed


Hey.

Line #45.
You fetch the first row into the $re variable on that line, so when you start your loop on line #55, the first row in the result is already gone.

If you comment line #45 out this particular problem should be fixed.

Although, on line #8 you have a random "endif;" which doesn't seem to belong anywhere. That should cause a parse error.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#3: 4 Weeks Ago

re: When printing SQL query, only the first result is not displayed


Also, just to throw it out there...

I would recommend avoiding the alternate control-structure syntax, and use the C-Like syntax instead. PHP is a C-Like language and should ideally be used as such.

By this I mean, instead of doing:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if ($something == "something"):
  3.   echo "Something!";
  4. else:
  5.   echo "Not something";
  6. endif;
  7. ?>
It is generally better to do:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if ($something == "something") {
  3.   echo "Something!";
  4. }
  5. else {
  6.   echo "Not something";
  7. }
  8. ?>
May seem irrelevant, but mixing syntax types can get very confusing.
Newbie
 
Join Date: Oct 2009
Location: Roll Tide
Posts: 2
#4: 4 Weeks Ago

re: When printing SQL query, only the first result is not displayed


Thanks Atli. I knew it was simple.

I used the control-structure syntax because that's how I first saw if else statements and while loops. I like using brackets because I'm use to them so I'll go back and change it.
Reply


Similar PHP bytes