Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_fetch_array(): Help with error needed

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
US
I've got some PHP web pages that connect to a MySQL database. I've got a query that deletes a record from a database, but it always generates the following error:

Code:
[b][COLOR=red]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/XXX.com/httpdocs/PROJECT1/process_authors.php on line 39[/color][/b]

The connection to the database is in an include file and works perfectly. The PHP code follows (since it's a MySQL error, I figured this would be the forum to post it in.):

Code:
  $result = mysql_query("SELECT * FROM MYAUTHORS where authorid = '$record_key'") or die("Error:.mysql_error());");

  $rows=mysql_affected_rows();
  if (!$result) {
     $message  = 'Invalid query: ' . mysql_error() . "\n";
     $message .= 'Whole query: ' . $query;
     die($message);
  } else {
[b][COLOR=red]while ($_POST = mysql_fetch_array($result, MYSQL_ASSOC))[/color][/b] {
        $id  = $_POST['id'];
        $authorid = $_POST['authorid'];
        $lname = $_POST['lname'];
        $fname = $_POST['fname'];
        $query ="DELETE from MYAUTHORS where authorid = '$record_key'";
        $result = mysql_query($query);
        $rows=mysql_affected_rows(); // returns number of rows produced by query.
        if (!$result) {
           $message  = 'Invalid query: ' . mysql_error() . "\n";
           $message .= 'Whole query: ' . $query;
           die($message);
        } else {
           if ($rows === 0) {
              echo "Record not found.<br>";
           }
        }
     }
  }

The code hilighted in red is what's generating the error message. The problem is that the code seems to work perfectly, as the correct record is deleted from the database, but the error message is still generated. If I put an at sign "@" infront of the mysql_fetch_array, it suppressed the error message. That's fine, but I'd really like to know what's causing the message and eliminate the cause. Any help would be greatly appreciated.
 
I don't know much about PHP but should that be a == rather than a = in your red highlighted line?

Andrew
Hampshire, UK
 
towerbase, That was an excellent idea, but unfortunately, if I use == or === instead of =, the test fails and the code isn't executed. The only one that passes the test is =. Thanks for the reply though!
 
I've solved this problem. The mistake was using the same $result variable for both queries. When the second query ran, it wiped out the value of $result from the first query. Even though the delete worked, MySQL didn't like this and threw the error. The solution is to use two unique result variables, this eliminates the error message.
 
I'd change your while to assign to an array other than $_POST, that holds the values from an HTML form field. Try something like $row. Why do you get values from the result set anyway as you delete the row without doing anything with the values (i.e. id,authorid,lname and fname). I think if you continue to use $_POST here or in your next code you are creating an error ready to get you in the future.
Towerbase, PHP is quite C like, so the loop will execute until the fetch returns a NULL, of course the left hand side gets populated with the result of the fetch.
So when peter used == the test will fail as $_POST and the row from the fetch won't match (unless by some incredible quirk of fate!)
 
I'd agree with ingresman-don't store stuff in $_POST. Its a special variable created for a special purpose. Using it like that is going to burn you in the future.

The accepted way of doing this is:
Code:
     while ( ($row = mysql_fetch_array($result, MYSQL_ASSOC) !== false) {

Which enforces strict type checking on the return value of mysql_fetch_array.

I'd also point out that your syntax on the first line looks like its not what you meant it to be:
Code:
die("Error:.mysql_error());");
//or
dice("Error:".mysql_error());

Robert Carpenter
Remember....eternity is much longer than this ~80 years we will spend roaming this earth.
ô¿ô
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top