|
Posted by alexbarham on 10/28/21 11:33
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
afterwards:
<title>Database Test</title>
</head>
<body>
<?php
$connection = mysql_connect('mydb','user','pass');
mysql_error();
mysql_select_db('mytable',$connection);
$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'];
$count++;
}
}
for($i=0;$i<$rowsfound-1;$i++)
{
echo "Close $i is: $close[$i]<br/>";
$i_plus = $i + 1;
echo "Close $i_plus is: $close[$i_plus]<br/>";
}
mysql_close($connection);
?>
</body>
</html>
I have tried something like:
for($i=0;$i<rowsfound;$i++)
{
echo "Value is $row['close'][$i];
}
This didn't work.
Thanks for your help
[Back to original message]
|