Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Linux, Apache, MySQL > MySQL Help

MySQL Help Post any question relating to MySQL here and hopefully someone can help

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2007-01-13, 09:53 AM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default mysql_fetch_array(): supplied argument is not a valid "Error"

I keep getting the above error, and have went over the script a hundred times. Can anyone see where I have made my mistake? I have put where the error is received in bold and underline

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/XXXX/public_html/update.php on line 54"

Code:
$cid=mysql_connect ("localhost", "XXXX", "XXXXX) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("XXXX", $cid);
    # processed when form is submitted back onto itself
    if ($REQUEST_METHOD=="POST") {
        # setup SQL statement
        $SQL = " UPDATE books SET";
        $SQL = $SQL . " Book = '$Book', ";
        $SQL = $SQL . " Year = '$Year', ";
        $SQL = $SQL . " Author = '$Author', ";
        $SQL = $SQL . " Genre = '$Genre' ";
        $SQL = $SQL . " Number = '$Number' ";
        $SQL = $SQL . " Series = '$Series' ";
        $SQL = $SQL . " Complete = '$Complete' ";
        $SQL = $SQL . " Recommendation = '$Recommendation' ";
        $SQL = $SQL . " Status = '$Status' ";
        $SQL = $SQL . " WHERE id = $id ";
 
        # execute SQL statement
        $result = mysql_db_query($db,"$SQL",$cid);
        # check for errors
        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }
        echo ("<P><B> Link Updated</B></P>\n");
    }
    else { # display edit form (not post method)
        # setup SQL statement to retrieve link
        # that we want to edit
 
        $SQL = " SELECT * FROM books ";
        $SQL = $SQL . " WHERE id = $id ";
        # execute SQL statement
        $ret = mysql_db_query($db,$SQL,$cid);
        # retrieve values
        $row = mysql_fetch_array($ret); ( Point of error)
        $Book = $row["Book"];
        $Year = $row["Year"];
        $Author = $row["Author"];
        $Genre = $row["Genre"];
        $Number = $row["Number"];
        $Series = $row["Series"];
        $Complete = $row["Complete"];
        $Recommendation = $row["Recommendation"];
        $Status = $row["Status"];
?>
Of course there is a form that goes with it, that form shows up fine.
Thanks in advance.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2007-01-13, 12:54 PM
Member
 
Join Date: Jan 2007
Location: SPb, Russia
Posts: 30
powerdesign is on a distinguished road
Default

first of all, why do you use mysql_db_query instead of mysql_query ?
the official documentation says that "This function is deprecated, do not use this function. Use mysql_select_db() and mysql_query() instead." (http://php.net/manual/en/function.mysql-db-query.php)

the second question is why do u use construction $result = mysql_db_query($db,"$SQL",$cid); 14 lines above and then $ret = mysql_db_query($db,$SQL,$cid); (here $SQL is not in "")
Reply With Quote
  #3 (permalink)  
Old 2007-01-13, 01:45 PM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default

Quote:
Originally Posted by powerdesign View Post
first of all, why do you use mysql_db_query instead of mysql_query ?
the official documentation says that "This function is deprecated, do not use this function. Use mysql_select_db() and mysql_query() instead." (http://php.net/manual/en/function.mysql-db-query.php)

the second question is why do u use construction $result = mysql_db_query($db,"$SQL",$cid); 14 lines above and then $ret = mysql_db_query($db,$SQL,$cid); (here $SQL is not in "")
I am not sure. This is a script that i am emulating to update or correct the database and it is copied from this website.

I have changed all that to this (got rid of the $db as well)
I still get the same error
Code:
if ($REQUEST_METHOD=="POST") {
        # setup SQL statement
        $SQL = " UPDATE `books` SET ";
        $SQL = $SQL . " Book = '$Book', ";
        $SQL = $SQL . " Year = '$Year', ";
        $SQL = $SQL . " Author = '$Author', ";
        $SQL = $SQL . " Genre = '$Genre' ";
        $SQL = $SQL . " Number = '$Number' ";
        $SQL = $SQL . " Series = '$Series' ";
        $SQL = $SQL . " Complete = '$Complete' ";
        $SQL = $SQL . " Recommendation = '$Recommendation' ";
        $SQL = $SQL . " Status = '$Status' ";
        $SQL = $SQL . " WHERE `Key` = $id ";
        # execute SQL statement
        $result = mysql_query($SQL,$cid);
        # check for errors
        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }
        echo ("<P><B> Link Updated</B></P>\n");
    }
    else { # display edit form (not post method)
        # setup SQL statement to retrieve link
        # that we want to edit
        $SQL = " SELECT * FROM `books` ";
        $SQL = $SQL . " WHERE Key = $Key ";
        # execute SQL statement
        $ret = mysql_query($SQL,$cid);
        # retrieve values
        $row = mysql_fetch_array($ret);
        $Book = $row["Book"];
        $Year = $row["Year"];
        $Author = $row["Author"];
        $Genre = $row["Genre"];
        $Number = $row["Number"];
        $Series = $row["Series"];
        $Complete = $row["Complete"];
        $Recommendation = $row["Recommendation"];
        $Status = $row["Status"];
Reply With Quote
  #4 (permalink)  
Old 2007-01-13, 01:55 PM
Member
 
Join Date: Jan 2007
Location: SPb, Russia
Posts: 30
powerdesign is on a distinguished road
Default

an error like "mysql_fetch_array(): supplied argument is not a valid MySQL result" gets when you have no results from query, for example.

try to debug your actions, say put this
echo 'the query is: '.$SQL.'<br /> and error: '.mysql_error(); die;
after your
# execute SQL statement
$ret = mysql_query($SQL,$cid);

and see if there any errors with you request to DB.
also you get you query text - check it, and if you have phpmyadmin, try to paste it there and executr sql query from phpmyadmin - look if there any errors.
Reply With Quote
  #5 (permalink)  
Old 2007-01-13, 03:28 PM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default

Thanks, that helped greatly in finding the syntax errors, I can at least open the page with no errors!

You were a great help, thank you. I find these things are usually right in front of you and you cant see them through the window.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.5 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.5 (a popular PHP IDE) and NuCoder 2.0 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:36 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.