mysql_num_rows giving error “mysql_num_rows() expects parameter 1 to be resource”

前端 未结 3 972
你的背包
你的背包 2021-01-25 07:39
public function doesUserExist($u) {

    $this->dbConnect();

    mysql_select_db($this->database);

    $sUser = mysql_real_escape_string($u);

    $query = \"SEL         


        
相关标签:
3条回答
  • 2021-01-25 08:12

    You have not actually executed your query:

    $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;
    }
    
    0 讨论(0)
  • 2021-01-25 08:18

    Actually you didn't executed query.

    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'";
        $query = mysql_query($query);
        $doesFieldExist = false;
    
        if (mysql_num_rows($query) > 0) {
            $doesFieldExist = true;
        }
    
        $this->dbDisconnect();
    
        return $doesFieldExist;
    
    }
    
    0 讨论(0)
  • 2021-01-25 08:27

    You should use the mysql_query before getting the number of rows.

    Follow the Link below: http://php.net/manual/en/function.mysql-num-rows.php

    For example :

    $query = "SELECT username FROM $this->table WHERE username='$sUser'";
    $result = mysql_query($query);  
    $num_rows = mysql_num_rows($result);  
    
    0 讨论(0)
提交回复
热议问题