在pdo中重写mysqli

问题描述 投票:0回答:2

我是Web编程的新手,会在下面的代码中询问建议。

我在数据库类中有代码。有。如您所见,mysqli与数据库建立了连接。并且此代码有效。

   function __construct() {
        $this->conn = $this->connectDB();
    }

    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
        return $conn;
    }

    function runBaseQuery($query) {
        $result = mysqli_query($this->conn,$query);
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }
        if(!empty($resultset))
            return $resultset;
    }



    function runQuery($query, $param_type, $param_value_array) {
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
        $result = $sql->get_result();
            if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $resultset[] = $row;
            }
        }
            if(!empty($resultset)) {
            return $resultset;
        }
    }

    function bindQueryParams($sql, $param_type, $param_value_array) {
        $param_value_reference[] = & $param_type;
        for($i=0; $i<count($param_value_array); $i++) {
            $param_value_reference[] = & $param_value_array[$i];
        }
        call_user_func_array(array(
            $sql,
            'bind_param'
        ), $param_value_reference);
    }

    function insert($query, $param_type, $param_value_array) {
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
    }

    function update($query, $param_type, $param_value_array) {
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
    }

我必须在PDO中编写此类。我已经做到了,但是出了点问题。我尝试连接我的数据库并收到错误

致命错误:未捕获的TypeError:PDO :: __ construct()需要参数4

class DB {
    private $host = "";
    private $user = "";
    private $password = "";
    private $database = "";
    private $pdo;


    function __construct() {
        $this->pdo = $this->connectDB();
    }

    function connectDB() {
        try
        {
            $pdo = new PDO($this->host,$this->user,$this->password,$this->database);
        }
        catch (PDOException $e) {
            print "Error!: " . $e->getMessage();
        }
        die();
    }

    function runBaseQuery($query)
    {
        $result = $pdo->query($query);
        while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
            $resultset[] = $row;
        }
        if (!empty($resultset))
            return $resultset;
    }



    function runQuery($query, $param_type, $param_value_array) {

        $sql = $pdo->prepare($query);
        $pdo->execute($sql, $param_type, $param_value_array);
        $result = $pdo->fetchAll(PDO::FETCH_ASSOC);

        if ($result->num_rows > 0) {
            while ($row = $pdo->fetchAll(PDO::FETCH_ASSOC)) {
                $resultset[] = $row;
            }
        }
        if(!empty($resultset)) {
            return $resultset;
        }
    }

    function bindQueryParams($sql, $param_type, $param_value_array) {
        $param_value_reference[] = & $param_type;
        for($i=0; $i<count($param_value_array); $i++) {
            $param_value_reference[] = & $param_value_array[$i];
        }
        call_user_func_array(array(
            $sql,
            'bind_param'
        ), $param_value_reference);
    }

    function insert($query, $param_type, $param_value_array) {
        $sql = $pdo->prepare($query);
        $pdo->execute($sql, $param_type, $param_value_array);
           }

    function update($query, $param_type, $param_value_array) {
        $sql = $pdo->prepare($query);
        $pdo->execute($sql, $param_type, $param_value_array);
           }
}

但是我的新代码不起作用。问题出在哪里?

php mysqli pdo
2个回答
1
投票

PDO连接没有第四个参数作为数据库名称。数据库名称与主机名一起传递。因此更改:

$pdo = new PDO($this->host,$this->user,$this->password,$this->database);

到:

$pdo = new PDO($this->host . ';dbname=' . $this->database, $this->user, $this->password);

有关更多信息,请参见https://www.php.net/manual/en/pdo.connections.php

此外,还不清楚$this->host包含什么,但对于PDO,如果这只是一个主机路径,您还需要传递您正在使用的驱动程序,那么您甚至可能需要添加]]

mysql:host=

到此开始。对于mysqli,这不是必需的,因为唯一的RDBMS mysqli支持的是mysql

因此可能是完整的答案:

$pdo = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database, $this->user, $this->password);

0
投票

您的新班级有多个问题。您所要解决的问题可以通过了解如何与PDO正确连接以及DSN是什么来解决。为此,我必须向您推荐这篇很棒的文章https://phpdelusions.net/pdo#dsn

© www.soinside.com 2019 - 2024. All rights reserved.