First off, let me state that I have read the FAQ about this error, and have tried all the fixes, with no success. If anyone can give me a hand with this I'd be grateful.

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:

The connection to the database is in an include file and works perfectly.

The exact error is:

[B]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[/B]

The connection to the database is in an include file and works perfectly. The PHP code follows:

$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]while ($_POST = mysql_fetch_array($result, MYSQL_ASSOC))[/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.

try this

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

Or, in plain English, you are overwriting the result of your SELECT query with the result of your DELETE query, which is not suitable for fetching rows from. Thus your script ABENDs at the top of the loop. (Geez I love using ancient terminology!)

Oh, and off-topic,

Why do Daniweb moderators edit CODE tags into CODE=style but don't check "solved".

DW mods know when code tags are 'incorrect'. But only the original poster can know when her problem has been solved.

turning (code) into (code=style) with NO button to do that anyway just just being (snip).

any it's fairly trivial and quite obvious when most posts are correct. FAR more obvious than style correction. Especially when the OP responds with, "that works great. thanks".

Not to mention "solved" posts add WAY more information to the forums than style change of type code, over default code type. Again, that's just being anal, and not helpful.

What the hell are you talking about?

turning (code) into (code=style) with NO button to do that anyway just just being (snip).

any it's fairly trivial and quite obvious when most posts are correct. FAR more obvious than style correction. Especially when the OP responds with, "that works great. thanks".

Not to mention "solved" posts add WAY more information to the forums than style change of type code, over default code type. Again, that's just being anal, and not helpful.

it's in response to the post above mine.

Also, if the code I posted worked, click solved.

Kireol, MY BAD. I apologize, I read the posts too quick and missed your other comments. I'm trying your code right now, and the first shot got an invalid query result, but the messages were blank. I'm checking it now for typos. Thank you for your post, and again I apologize for the inconsiderate answer.

Kireol,
Your solution works PERFECTLY! Thank you, thank you, thank you! The reason it didn't work the first time was I made a typo. I'm a newbie to PHP, and I really appreciate your taking the time to analyze the problem and explaining it for me. I hope when I get a little more experience, I can return the favor.
Peter V.

it's all good.

The error you made could have been made by a newbie or a seasoned veteran. It's a hard error to see. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.