如何在2个PHP类中的2个函数上返回mysqli connect_error

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


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;

    }

    

}


}

?>

测试.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)。我怎样才能做到这一点?


千巷猫影
浏览 62回答 1
1回答

慕慕森

在目前的状态下,它对你来说根本没有用处。mysqli 连接始终是相同的三行代码,没有必要为其添加类。要使用 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然后你可以编写一个类,它将数据库连接作为参数__construct()。class Test {    private \mysqli $db = null;    public function __construct(\mysqli $db) {        $this->db = $db;    }    function runQuery($sql) {        $query = $this->db->query($sql);    }}就是这样。尽管您确实应该尝试创建一个更有用的 mysqli 抽象类,或者更好,但使用 PDO 代替。如果你想编写 mysqli 包装类,你可以从以下想法开始(根据你的需要进行调整):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;    }}然后,即使使用 mysqli,您也可以用简单的一行执行任何 SQL 语句。
打开App,查看更多内容
随时随地看视频慕课网APP