473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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>
Oct 30 '09 #1
3 3113
Atli
5,058 Recognized Expert Expert
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.
Oct 30 '09 #2
Atli
5,058 Recognized Expert Expert
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.
Oct 30 '09 #3
jmooney5115
2 New Member
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.
Oct 30 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1842
by: sarith sutha | last post by:
Hi Guys Need a help i am facing a sporadic issue when executing the query using nested table of numbers Here are the things which i did 1. I created a type as CREATE TYPE NUMBERTABLE AS...
8
63306
by: Marcus | last post by:
Say a number of people have 2 or 3 addresses each in a query result. How do I select only the first address displayed for each person. For example: John Taylor 33 Dundas Ave. John Taylor 55...
0
2819
by: Jim French | last post by:
I have a page that takes a comma delimited string and needs to bind to a Repeater control. The string is split into an Array, and each value is put into a DataBase query via a For Each loop. The...
7
3509
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that...
1
3893
by: JackM | last post by:
I'm not sure if this qualifies as a mysql or a php question so I'm asking in both groups. I am pulling the results of a mysql query from my database and want to print the results into a two...
2
2042
by: DavidPr | last post by:
I'm creating (trying to create) a picture gallery for my website. The script is not working. I've been working on it now for about 80 hours with no success. My php skills aren't very good. This...
3
1578
by: Terry | last post by:
Hi folks. I am just starting to dabble in php. I am trying to do some simple pagination in php/mysql. For whatever reason I am only getting the first digit of my result. For example: ...
1
1345
by: launchpad67a | last post by:
Hello, I'm retrieving database info and turning it into an xml file. The xml file has to be structured exactly like this example below: Here is what I need: <xml> <listitem name="Video 1"...
10
2631
by: Lloyd Harold | last post by:
I'm very new to PHP and attempting to put together a simple script for retrieving MySQL data of personal records. The MySQL table I'm using consists of: 0: id 1: name 2: location (an integer...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.