如何在php OOPs中为mysqli_num_rows创建用户定义的函数?

时间:2014-03-06 11:52:06

标签: php

我尽力自己找出结果,但失败了。

这是我在我的关卡中尝试过的源代码。

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

请指导我在哪里弄错了。

由于

2 个答案:

答案 0 :(得分:0)

您拥有$numRows类属性。为什么你没有使用它?

将查询行数设置为$this->numRows属性。

更改select()方法:

 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 . "'";
            }
          $result = mysqli_query($this->connect, $query);

            if ($result) {
                $this->numRows = mysqli_num_rows($result);
                return true;
            } else {
                die(mysqli_error($this->connect));
            }
        }
    }

并改变您的countRows()方法,例如yhis:

 public function countRows($queryRes)
        {
            return $this->numRows == 0 ? false : true; 
        }

答案 1 :(得分:0)

function countRows($sql)
{
   $rst              =  @mysql_query($sql)or trigger_error("SQxxxL", E_USER_ERROR);
   return $numrows       =  mysql_num_rows($rst);
}