Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error in while statement

Posted on 2003-12-01
4
Medium Priority
?
184 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
Suggested Courses

824 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