如何 return mysqli connect_error over 2 function in 2 类 PHP

How to return mysqli connect_error over 2 function in 2 classes PHP

我有 dbc.inc.php 文件。它里面有连接功能,可以将我连接到数据库。 在 test.inc.php 文件中,我在“测试”class 中有 runQuery 函数。 “Test”class 扩展自 dbc.inc.php 文件中分配的“Dbc”class。

runQuery($db, $sql) 运行查询。但是,如果发生错误或发出警告,他不会显示错误。我相信我有语法错误。

出于测试兴趣,我在 $sql 语句中给出了错误的字段名。错误正在发生但未显示。

dbc.inc.php

<?php

class Dbc{
private $serverName;
private $userName;
private $password;

protected function connect($dbName = NULL){
    $this->serverName = "localhost";
    $this->userName   = "root";
    $this->password   = "";
    
    $conn = new mysqli($this->serverName, $this->userName, $this->password, $dbName);
    if (!$conn) {
        die("<h3>Error Connecting to the Database.</h3><h4 style=\"color: red\">". $conn->connect_error . "</h4>");
        
        
    } else {
        return $conn;
    }
    
}

}
?>

test.inc.php

<?php

 require 'dbc.inc.php';

 class Test extends Dbc{

function runQuery($db, $sql){
    $query = mysqli_query($this->connect($db), $sql);
    if (!$query) {
        echo "no Query";
        echo $this->connect($db)->connect_error;
        return 0;
    } else {
        echo "Query EXEC";
        return 1;
    }
}

 }


?>

测试代码

$conn = new Test;
$conn->runQuery("tch_phn", "UPDATE `employee` SET `uiS`='Assad' WHERE `uid`='Assad' ");

错误是我给了一个未知的字段名称(uiS 必须是 uid)。我该怎么做?

您不需要 Dbc class。在当前状态下,它对您根本没有用。 mysqli连接总是一样的三行代码,没必要给它加个class

要使用 mysqli 连接到数据库,请使用:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db_name');
$mysqli->set_charset('utf8mb4'); // always set the charset

然后你可以写一个class,它会将数据库连接作为__construct()中的参数。

class Test {
    private \mysqli $db = null;
    public function __construct(\mysqli $db) {
        $this->db = $db;
    }

    function runQuery($sql) {
        $query = $this->db->query($sql);
    }
}

就是这样。尽管您确实应该尝试创建更有用的 mysqli 抽象 class,或者甚至更好,但请改用 PDO。如果你想编写 mysqli wrapper class 你可以从以下想法开始(根据你的需要调整):

class DBClass extends mysqli {
    public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) {
        // Enable error reporting and call mysqli constructor
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
        $this->set_charset('utf8mb4'); // always set the proper charset which should be utf8mb4 99.99% of the time
    }

    public function safeQuery(string $sql, array $params = []): ?array {
        // Prepare statement:
        $stmt = $this->prepare($sql);
        // If the statement has parameters then bind them all now
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        // Execute it and get results if there are any
        $stmt->execute();
        if ($result = $stmt->get_result()) {
            return $result->fetch_all(MYSQLI_BOTH);
        }
        // If the query was INSERT or UPDATE then return null
        return null;
    }
}

然后你可以用简单的一行执行任何SQL语句,即使使用mysqli。