警告:mysqli_num_rows()期望参数1为mysqli_result,给定对象 [英] Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given

查看:155
本文介绍了警告:mysqli_num_rows()期望参数1为mysqli_result,给定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用(MySQLi Procedural)将我的php分成了2/3件: 首先专用于db(打开,执行,总行,受影响的行,关闭..) 其次是致力于其他功能. 这是一个示例:

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(第一个)

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);
}

}
?>

和 functions.php(第二个)

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

如果您查看此函数以获取行数,则会在函数内部测试参数,则sql语句已直接在mysql服务器中进行了测试,没有任何错误.

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.

有什么问题吗?

推荐答案

您不希望传递结果,而不是传递连接

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

$result = $db->Qry($sql);
$no_of_rows =  $db->TotRows($result);

这篇关于警告:mysqli_num_rows()期望参数1为mysqli_result,给定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆