2015-04-02 107 views
0

我已经使用(MySQLi程序)将我的php分成2/3个部分: 首先是致力于数据库(打开,执行,总行,受影响的行,关闭..) 其次是致力于其他功能。 下面是一个例子:警告:mysqli_num_rows()期望参数1是mysqli_result,对象给出

db.php中(一)

<?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(二)

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

当我尝试执行我得到了以下警告脚本错误:

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

如果你看这个函数来获取行数,参数是在函数内部测试的,那么sql语句已经直接在mysql服务器上测试过了,没有任何错误。

任何想法是什么问题?

回答

1

不要你wan't的结果通过,而不是连接

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

我明白了,非常感谢 – koul 2015-04-02 13:08:11

相关问题