Solved

Error in while statement

Posted on 2003-12-01
4
179 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

617 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