Solved

Error in while statement

Posted on 2003-12-01
4
177 Views
Last Modified: 2010-05-18

Hi,

I seem to have an error in the while statement below, my 'print' prints that line three times instead of once, the proceeds to the next, then again prints it three times before moving on to the next, and so on...

I don't have much exp with this, just adapted it from another script, can you tell me what is wrong?

Thanks,     Chris

--------------------------

//get data

$query = "SELECT * FROM user where Number > 0 order by Number";

$result = MYSQL_QUERY($query);

$number = MYSQL_NUMROWS($result);

/*display results*/

$i = 0;

IF ($number == 0) :
PRINT "ERROR, database seems to be out to lunch.  Drop a note to Chris and he will look into it.";
ELSEIF ($number > 0) :
WHILE ($i < $number):
$ordernumber = mysql_result($result,$i,"Number");
$account = mysql_result($result,$i,"Account");
$order_date = mysql_result($result,$i,"Order_Date");
$name = mysql_result($result,$i,"Name");
$status = mysql_result($result,$i,"Status");
$month1 = mysql_result($result,$i,"$date1");
$month2 = mysql_result($result,$i,"$date2");
$month3 = mysql_result($result,$i,"$date3");
$month4 = mysql_result($result,$i,"$date4");

PRINT "<tr><td>$ordernumber</td><td>$account</td><td>$order_date</td><td>$name</td><td>$status</td><td>$month4</td><td>$month3</td><td>$month2</td><td>$month1</td></tr>";


$i++;  
ENDWHILE;
ENDIF;
0
Comment
Question by:ChrisAndrews
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 75 total points
ID: 9850500
hi,

there's a better way to do this:

IF ($number == 0) :
  PRINT "ERROR, database seems to be out to lunch.  Drop a note to Chris and he will look into it.";
ELSEIF ($number > 0) :
  while($row = mysql_fetch_assoc($result)) {
    $ordernumber = $row["Number"];
    $account = $row["Account"];
    $order_date = $row["Order_Date"];
    $name = $row["Name"];
    $status = $row["Status"];
    $month1 = $row["date1"];
    $month2 = $row["date2"];
    $month3 = $row["date3"];
    $month4 = $row["date4"];

    PRINT "<tr><td>$ordernumber</td><td>$account</td><td>$order_date</td><td>$name</td><td>$status</td><td>$month4</td><td>$month3</td><td>$month2</td><td>$month1</td></tr>";
}
ENDIF;

that should work for you

cheers,

loz
0
 

Author Comment

by:ChrisAndrews
ID: 9850890

Hi loz,

Thank you, I am still getting three of each 'print' line, hmmm, what could be causing this?  

oh, just checked the database, phpmyadmin reports 134 rows when there should 45.

 When I browse the rows there is only 45 rows.  I don't know what is happening there, I may need to rebuild the table?

Any thoughts?   I will send these points to you because the suggested script is good, if you have any thoughts about
this let me know :)

Chris
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9850941
best thing to do would be to dump the table and to look through it to see what's going wrong, paste it here if you want help identifying the problem

loz
0
 

Author Comment

by:ChrisAndrews
ID: 9853186

ah loz, your so smart :)  dumped the table, found multiple entries, cleaned it out and all is well.... course, now I will run my scripts again and try to figure out what goofed it up in the first place.  May have just been me testing over and over did something in.

Thank you,

Chris
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question