开发者

PHP mysql_query, integer returned instead of resource

Before anyone jumps on me, I have found a similar issue here, but unfortunately their answer does not seem to apply to my problem.

I have created a function called sqlReturn() in order to more easily produce an error (with standard output) should a query go wrong. The code is below:

function sqlResult开发者_开发知识库($query)
{
    return mysql_query($query) 
    or die("SQL Query: " . $query . "<br />SQL Error: " . mysql_error());
}

As you can see, it just outputs an error in the way I like, and it saves me a bit of effort in coding along the way. However, while this has been working in most cases (eg. situations where I use SELECT or INSERT), it is throwing the following error:

PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, 
              boolean given in /var/www/login/login_submit.php on line 42

It is returning 1 instead of a resource. If, instead of calling that function (which is in a separate php file), I simply use the line of code in the same file without a return statement

(ie. $sqlResult = mysql_query($sqlQuery) or ... etc.), it returns a resource as normal.

In case it's relevant, my SQL query is also below:

$sqlQuery = 
  "SELECT userID, username, password, access_level
   FROM users
   WHERE username = '{$username}'
   AND (password = '{$password_sha1}' OR password = '{$password_sha256}')";

Any input on this would be appreciated.

Thanks,

Paragon


Sneaky suspicion that binding rules are kicking in here. PHP may be seeing your function as

return (mysql_query(...)) or die(...);

and return before ever seeing the die(). Try rewriting like this

function sqlQuery(...) {
    $result = mysql_query(...);
    if ($result === FALSE) {
        die(mysql_error(...));
    }
    return $result;
}

so there's no chance of any mis-parsing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜