mysql_fetch_array for loop

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am working on a script that queries a database and displays the
results as a line graph. Is there a way to loop through the results
using a for loop rather than the while loop? I have attached code that
just simply prints the values. I will worry about creating a graphic
<title>Database Test</title>
$connection = mysql_connect('mydb','user','pass');
$query = "SELECT date,symbol,close FROM stock_chart WHERE symbol='AA'";
$result = mysql_query($query,$connection);
$rowsfound = mysql_num_rows($result);
if($rowsfound > 0)
        $count = 0;
        while($row = mysql_fetch_array($result))
                $close[$count] = $row['close'];

        echo "Close $i is: $close[$i]<br/>";
        $i_plus = $i + 1;
        echo "Close $i_plus is: $close[$i_plus]<br/>";

I have tried something like:
       echo "Value is $row['close'][$i];
This didn't work.

Thanks for your help

Re: mysql_fetch_array for loop

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Re: mysql_fetch_array for loop

Probably easier to just remember the previous value as your loop
through the array.

$prev_value = -1;
$prev_date = 0;
foreach($close as $date => $value) {
   if($prev_value >= 0) {
      // draw line from closing value from previous date
      // current value
   else {
      $first_date = $date;
   $prev_value = $value;
   $prev_date = $date;

I'm using the date as the index here, since that'll probably be needed
for drawing the chart.

Site Timeline