php mysqli oops 概念连接

标签 php mysql function class oop

这个问题在这里已经有了答案:





mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

(31 个回答)


6年前关闭。




config.php 文件

<?php
Class Config
{
    private $_host;
    private $_user;
    private $_password;
    private $_db;

    public function __constructor()
    {
        $this->_host = 'localhost';
        $this->_user = 'root';
        $this->_password = '';
        $this->_db = 'test';
    }

    public function connectDB()
    {
        $connect = new mysqli($this->_host, $this->_user, $this->_password, $this->_db);
        if (!$connect) {
            die('Connection Error : ' . $connect->mysql_error());
        } else {
            echo 'Connection Success';
        }
        return $connect;
    }
}
?>

index.php 文件
<?php
include 'config.php';
$conn = new Config();
$conn = $conn->connectDB();
$sql = "SELECT name, matric_no FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo 'Name : ' . $row['name'] . ' - Matric No : ' . $row['matric_no'] . '<br>';
    }
} ?>

显示错误:

Connection Success
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\test\index.php on line 10



有什么解决办法吗?

最佳答案

构造函数应为:

  • public function __construct()
  • 而不是 public function __constructor()

  • 根据手册:http://php.net/manual/en/language.oop5.decon.php关于构造函数和析构函数
    <?php
    Class Config
    {
        private $_host;
        private $_user;
        private $_password;
        private $_db;
    
        public function __construct()
        {
            $this->_host = 'localhost';
            $this->_user = 'root';
            $this->_password = '';
            $this->_db = 'test';
        }
    
        public function connectDB()
        {
            $connect = new mysqli($this->_host, $this->_user, $this->_password, $this->_db);
            if (!$connect) {
               return 'Connection Error : ' . $connect->mysql_error();
            } else {
               return $connect;
            }
    
        }
    }
    ?>
    

    并在 Index.php 文件中通过 var_dump/print_r 检查连接是否成功
    <?php
    include 'config.php';
    $conn = new Config();
    $conn = $conn->connectDB();
    print_r($conn); break; //Remove this line if db connxn is succesfull
    
    $sql = "SELECT name, matric_no FROM users";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo 'Name : ' . $row['name'] . ' - Matric No : ' . $row['matric_no'] . '<br>';
        }
    } ?>
    

    将执行流程牢记在心,逐点调试以找出问题所在。

    关于php mysqli oops 概念连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35476770/

    相关文章:

    php - PHP 中的 LDAP 身份验证 - 在不提供密码的情况下进行身份验证

    php - 如何使用 Laravel 5 登录用户

    php - 如果 mysql 中存在记录,如何构建 php 代码以填充表单中的 280 个字段

    Python - 多个函数 - 输出一个到下一个

    在单独的 .cpp 文件和 makefile 链接中在类外部定义的 c++ 函数

    javaScript - 将对象作为函数参数传递

    php - 使用 codeigniter 在表中显示数据

    php - 无法加载资源: the server responded with a status of 449 (Woocommerce)

    mysql - 在sql中存储多对多分层数据的最佳方式

    php - 如何根据 MySQL 数据库中的禁用词检查输入?