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
https://forums.phpfreaks.com/topic/100874-solved-while-loop-error/
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

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.