473,597 Members | 2,726 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 3123
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
1850
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 TABLE OF NUMBER;
8
63389
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 Shane Ave. John Taylor 786 Edward St. Ted Charles 785 Bloor St. Ted Charles 90 New York Ave.
0
2826
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 purpose is to retrieve address information for the user. Sometimes, the address_fk field in the people table is null, so there will be no linking address information. In this case, the section to be displayed asks if the user would like to create...
7
3520
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 performs MySQL queries to a database. These queries are performed at regular intervals. Basically it is looking for fields that match certain criterias. Such fields are not present at all time. When the program finds such field, it then takes
1
3901
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 column table. I know how to get the results into a single column table just fine using: while($row = mysql_fetch_array($result)) { print "<table border=2><tr><th>" . $row; print "<tr><td>";
2
2048
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 picture gallery uses 2 mysql tables: acwrtcats and acwrtpics The table acwrtcats has 2 fields: cat_name and cat_desc. The cat_name is the name of a category that pictures will be filed under, and cat_desc is a brief description of the category. ...
3
1583
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: $result = mysql_query("select lamp_no from lamps as l where id=20");
1
1348
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" url="files/flv" thumb="my_video1.jpg"> <stream name="my_video1.flv" start="0" len="-1" /> </listitem> <listitem name="Video 2" url="files/flv" thumb="my_video2.jpg">
10
2638
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 relating to a separate table of locations). 3: details
0
7979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8281
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6706
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
1497
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.