Jump to content

[SOLVED] Looping Problem


flyersun

Recommended Posts

When I started writing this script it sounded pretty simple but I've run into a few problems.  The script some information from a database and then a loop is used to put the information into variables.  What I want is for each time the loop is run it outputs different variables so that these variables can then be used later on in the script. 

 

So for example at the moment each time it goes around a different value is assigned to $data1 etc but I want it to create a new variable to put the data into instead of just writing over the old one.

 

$querydata = "SELECT * FROM tblgraphdata WHERE graphID ='1'";

 

$resultsdata = mysql_query($querydata) or die ("Problem with query:" . mysql_error());

 

$rowsdata = mysql_num_rows($resultsdata);

 

$i = 0;

 

while ($i < $rowsdata){

 

$data1 = mysql_result($resultsdata, $i, "data1");

$data2 = mysql_result($resultsdata, $i, "data2");

$data3 = mysql_result($resultsdata, $i, "data3");

$data4 = mysql_result($resultsdata, $i, "data4");

$color = mysql_result($resultsdata, $i, "color");

$i ++;

 

}

 

Any ideas?

 

Thank you soo much for helping!

 

Link to comment
https://forums.phpfreaks.com/topic/86825-solved-looping-problem/
Share on other sites

You'll find it easier if you use mysql_fetch_assoc rather than mysql_result:

$querydata = "SELECT data1, data2, data3, data4, color FROM tblgraphdata WHERE graphID ='1'";
$resultsdata = mysql_query($querydata) or die ("Problem with query:" . mysql_error());


while ($row = mysql_fetch_assoc($resultsdata))
{
    $rowdata[] = $row; // add returned results from $row intop $rowdata array.
}

echo '<pre>' . print_r($rowdata, true) . '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.