我盡我所能,自己找出結果,但都失敗了。如何在PHP OOP中爲mysqli_num_rows創建用戶定義的函數?
這裏是我嘗試過的最好的源代碼。
<?php
class DataBase
{
private $connect;
private $dbUser;
private $dbHost;
private $dbPassword;
private $dbDatabase;
private $numRows;
private $results;
public function connect($host, $username, $pass, $db)
{
$this->dbHost = $host;
$this->dbUser = $username;
$this->dbPassword = $pass;
$this->dbDatabase = $db;
return $this->connect = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbDatabase);
}
public function disConnect($connect) {
return mysqli_close($this->connect = $connect);
}
public function select($table, $where = array(), $orderBy = NULL) {
if (count($where) === 3) {
$operators = array('=', '<', '>', '>=', '<=', 'LIKE');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if (in_array($operator, $operators)) {
$query = "SELECT * FROM {$table} WHERE '". $field . $operator . $value . "'";
}
if (mysqli_query($this->connect, $query)) {
return true;
} else {
die(mysqli_error($this->connect));
}
}
}
public function countRows($queryRes)
{
if (mysqli_num_rows($queryRes) > 0) {
return true;
} else {
return false;
}
}
}
我得到的錯誤:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\Practice\index.php on line 70
和線路70:if (mysqli_num_rows($queryRes) > 0) {
這裏就是我如何調用該方法:
$suc = "";
if (isset($_POST['btnSubmit'])) {
$con = new DataBase();
$objDb = $con->connect('localhost', 'root', '', 'practice');
$username = $_POST["username"];
$suc = $con->select('users', ['username', 'LIKE', '%'.$username.'%']);
if ($suc) {
print_r($con->countRows($suc));
} else {
echo "Unable to Find Record !";
}
//print_r($suc);
$con->disConnect($objDb);
}
?>
請指引我我在哪裏犯錯誤。
感謝
()? –
你在哪裏調用countRows()? –
你正在從選擇函數返回布爾值,並且我猜你正在將select函數結果傳遞給countRows,但是'mysqli_num_rows'希望你傳遞mysqli_query而不是boolean –