dbConnect(); mysql_select_db($this->database); -6ren">
gpt4 book ai didi

php - mysql_num_rows 给出错误 "mysql_num_rows() expects parameter 1 to be resource"

转载 作者:行者123 更新时间:2023-11-29 04:53:01 27 4
gpt4 key购买 nike

public function doesUserExist($u) {

$this->dbConnect();

mysql_select_db($this->database);

$sUser = mysql_real_escape_string($u);

$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$doesFieldExist = false;

if (mysql_num_rows($query) > 0) {
$doesFieldExist = true;
}

$this->dbDisconnect();

return $doesFieldExist;

}

我在这一行 (60) 上收到错误

if (mysql_num_rows($query) > 0) {

错误是:

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\Users\Tom\Dropbox\public_html\classes\database.class.php on line 60

对于我正在使用的查询,mysql_num_rows($query) 应该返回 1。我在这里进行了 google 和检查,但看不出我做错了什么。

最佳答案

您还没有真正执行您的查询:

$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$doesFieldExist = false;

// Execute the query with mysql_query()
$result = mysql_query($query);

// $result is a result resource that can be passed
// to mysql_num_rows() unless the query failed and $result is FALSE
if ($result && mysql_num_rows($result) > 0) {
$doesFieldExist = true;
}

关于php - mysql_num_rows 给出错误 "mysql_num_rows() expects parameter 1 to be resource",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9548791/

27 4 0