亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在2個PHP類中的2個函數上返回mysqli connect_error

如何在2個PHP類中的2個函數上返回mysqli connect_error

PHP
千巷貓影 2023-07-08 17:50:58
我有 dbc.inc.php 文件。它里面有連接函數將我連接到數據庫。在 test.inc.php 文件中,我在“Test”類中有 runQuery 函數。“Test”類擴展自 dbc.inc.php 文件中分配的“Dbc”類。runQuery($db, $sql) 運行查詢。但是,如果發生錯誤或警告,他不會顯示錯誤。我相信我有一個語法錯誤。為了測試興趣,我在 $sql 語句中給出了錯誤的字段名。錯誤正在發生但未顯示。dbc.inc.php<?phpclass 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)。我怎樣才能做到這一點?
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

在目前的狀態下,它對你來說根本沒有用處。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 語句。


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號