rows创建用户定义的函数

rows创建用户定义的函数

本文介绍了如何在php OOPs中为mysqli_num_rows创建用户定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽力找到了我自己的结果,但都失败了。



这是我到目前为止尝试的源代码:



I have tried my best to find out the result on my own, but have failed.

Here's the source code I've tried so far:

<?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 . "'";
                
            }

            $this->results = mysqli_query($this->connect, $query);

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

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







这是我调用这些方法的地方和方式:






Here's where and how I call the methods:

$suc = "";
$name = "";
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 > 0) {
        print_r($con->countRows());
    } else {
        echo "Unable to Find Record !";
    }
    //print_r($suc);
    echo $name;

    $con->disConnect($objDb);
}
?>





Am得到错误无法找到记录!



请解决我的疑问。



谢谢。



Am getting error as Unable to Find Record !

Kindly solve my query.

Thanks.

推荐答案




这篇关于如何在php OOPs中为mysqli_num_rows创建用户定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 13:24