Warning: mysqli_num_rows() expects parameter 1 to

2020-05-07 05:38发布

I have splitted my php into 2/3 pieces using (MySQLi Procedural) : First is dedicated to db (open,execute,Total Row, Affected Row,Close..) Second is dedicated to other functionalities . Here is an example :

db.php (First)

<?php
class DB {
var $DBUser = 'myuser';
var $DBPass = 'mypassword';
var $DBServer = 'localhost';
var $DBName = 'myDB';
var $con;

function __construct() {
    $testcon = mysqli_connect($this->DBServer, $this->DBUser, $this->DBPass,$this->DBName);                               

    if (!$testcon) {
      die('Database connection failed: '  . mysqli_connect_error());
    } else {
        $this->con = $testcon;
    }
}

function Qry($sql) {
    if($result = mysqli_query($this->con,$sql) ) {
        return $result;
    }
    else 
    {
        $err = "Error: ".$sql. " :: ". mysqli_error;
        die("$err");
    }
}

function TotRows($result) {
    if($result === false) 
    { 
        die("Error ".mysqli_error); 
    }
    else    return mysqli_num_rows($result);
}

function AffRows($result) {
    return mysqli_affected_rows($result);
}

function LastRow($tblName) {
    return mysqli_insert_id($this->con);
}


function close() {
    mysqli_close($this->con);
}

}
?>

and functions.php (Second)

public function GetBoolResult($db,$sql) {
        $result=$db->Qry($sql);
        $no_of_rows =  $db->TotRows($db->con);

        if ($no_of_rows > 0) {
            // user exist
            return true;
        } else {
            // user does not exist
           return false;
        }
 }

When I try to execute the script I got the following Warning error :

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in db.php

if you look at this function to get number of rows , the parameter is tested inside the function, the sql statement has been tested directly in the mysql serverwithout any error.

Any idea what's the problem ?

标签: php mysqli
1条回答
一夜七次
2楼-- · 2020-05-07 06:20

Don't you wan't to pass in the result, not the connection

$result = $db->Qry($sql);
$no_of_rows =  $db->TotRows($result);
查看更多
登录 后发表回答