Jump to content

[SOLVED] while loop error


anser316

Recommended Posts

I have a loop within a table, that display posted variables that have been checked. That works, but when i get another value from mysql, its hard to put that in a result as i am looping it.

what i am trying to do is this:

1. select a value from a table. 2. put in a variable. 3. display. 4. increment

5. select different value 6. put in variable etc.

 

I get a warning message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:...conorder.php on line 45.

The table shows up and the other data displays correctly, its just the status. that shows a blank with the warning message

 

 

I havent been able to do the standard while loop as i am only intending to get one result, increment, then get another. 

 

for ($i=0; $i<count($_POST['ticked']); $i++) {
$row_value = $_POST['ticked'][$i];

      $result2 =mysql_query("SELECT status from branch_items
WHERE drug_id=".$_POST['ticked'][$row_value]."
AND branch_id=".$_POST[branch_id][$row_value]."");

$row = mysql_fetch_array( $result2 );    //  LINE 45

  echo "<tr><td>"; 
        echo $_POST[drug_id][$row_value];
        echo "</td><td>";
        echo $_POST[branch_id][$row_value];
        echo "</td><td>";
        echo $row[status];
  echo "</td></tr>"; 
    }
echo "</table>";
}

Link to comment
Share on other sites

Well, obviously the query is failing. But, you have no error handling to catch it. You should add " or die (mysql_error()): at the end of the query. The error in the query is due to the double quote marks at the end.

 

But, you should also not have a looping query. Just grab all the records in one query:

 

Sample code:

<?php
$tickedkValues = implode(',', $_POST['ticked']);

$query = "SELECT status
          FROM branch_items
          WHERE drug_id IN ($tickedkValues)
            AND branch_id=".$_POST[branch_id][$row_value];

$result = mysql_query($query) or die ("Query:<br>$query<br>Error:<br>".mysql_error());

echo "<table>";
while ($row = mysql_fetch_array( $result2 ))
{
  echo "<tr>"; 
  echo "<td>".$_POST[drug_id][$row_value]."</td>";
  echo "<td>".$_POST[branch_id][$row_value]."</td>";
  echo "<td>".$row[status]."</td>";
  echo "</tr>"; 
}
echo "</table>";
?>

 

You also need to "clean" all the values before running a query on them

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.