Solved

Error in while statement

Posted on 2003-12-01
4
169 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
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 75 total points
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now