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
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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.