开发者

PHP+mySQL trouble. supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate] (6 answers) Closed 9 years ago.

Please look below for edit

Hi, everyone. My error is on my while loop. I've had to add a @ to it to make it stop giving me the warning. It works just fine. Everything updates perfectly. But, rather than kludge it, I'd really like to not to have to hide my errors.

I just have no idea what is wrong with it. Like I said, my while loop (line 113) is giving me this:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xx/xxxxxxx/xxx/admin/price_chng.php on line 113 

Here's the code.

if($manu < 15){
    $row_count = 0;
    $query = "SELECT * FROM `phone_models` WHERE `manufacture_id`=" . $manu . ";";
    $result = mysql_query($query);   
    while($row = mysql_fetch_array($result)){
        if($row_count < 5){
            echo ("<form action='price_chng.php' method='post' name='newprice' id=" . $row['id'] . ">");
            echo "<td>" . $row['label'] . "<br>";
            echo("<input name='newprice' type='text' id=" . $row['id'] . " size='10' value=" . $row['buyback_price'] . "><br>");
            echo("<input type='hidden' name='ud_id' value='$row[id]'>");
            echo("<input name='doSave' type='submit' id='doSave' value='Save'></form></td>");
            $row_count++;
        }else{
            $row_count = 0;
            echo("</tr><tr>");
        }
    }   
}else{
    echo("</tr></td>");
}

Any help is appreciated! Thanks, guys!

EDIT: Updated the code. I took out the or die from my statement because I wanted the warning to stop. I forgot to put it back in. WHen I put it back in, I have this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

And, my code is:

if($manu < 15){
    $row_count = 0;
    $query = "SELECT * FROM `phone_models` WHERE `manufacture_id`=" . $manu . ";";
    $result = mysql_query($query) or die(mysql_error());     
    while($row = mysql_fetch_array($result)){
        if($row_count < 5){
            echo ("<form action='price_chng.php' m开发者_运维技巧ethod='post' name='newprice' id=" . $row['id'] . ">");
            echo "<td>" . $row['label'] . "<br>";
            echo("<input name='newprice' type='text' id=" . $row['id'] . " size='10' value=" . $row['buyback_price'] . "><br>");
            echo("<input type='hidden' name='ud_id' value='$row[id]'>");
            echo("<input name='doSave' type='submit' id='doSave' value='Save'></form></td>");
            $row_count++;
        }else{
            $row_count = 0;
            echo("</tr><tr>");
        }
    }   
}
else{
    echo("</tr></td>");
}


It means exactly what it says. Your MySQL result (from mysql_query) is invalid. mysql_fetch_array expects a valid MySQL result resource, which is why an error is thrown.

I'd try changing:

$result = mysql_query($query);

To:

$result = mysql_query($query) or die(mysql_error());

The script should die and print out the MySQL error that is causing your query to fail.

Are you filtering your MySQL data? ($manu = mysql_real_escape_string($manu)) - Also you may want to wrap $manu in quotes.

..

In response to your comment, (although the mysql_error response can be cryptic) I'd try modifying the script as follows:

Replace:

$query = "SELECT * FROM `phone_models` WHERE `manufacture_id`=" . $manu . ";";

With (2 lines):

$manu = mysql_real_escape_string($manu);
$query = "SELECT * FROM phone_models WHERE manufacture_id = '" . $manu . "'";

I'd guess it's one of the following things causing your query to fail:

  1. $manu is unfiltered, and could contain any number of things that would make the query fail
  2. $manu is empty or null, and not wrapped resulting in (SELECT * FROM phone_models WHERE manufacture_id = )


Add or die(mysql_error()); to the end of the call to mysql_query() and see what it says.

There is nothing wrong with your SQL, except for possibly the semicolon at the end. The standard MySQL library for PHP does not support multiple SQL statements in a single SQL call, and therefore may be causing it to throw the warning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜