问题
I have the dbc.inc.php file. inside of it there are connect function that connects me to the DB. In the test.inc.php file i have the runQuery function inside of a "Test" class. The "Test" class extends from the "Dbc" class allocated in the dbc.inc.php file.
The runQuery($db, $sql) runs query. But if error happend or warning he is not showing the error. i belive that im having a syntax mistake.
For the testing interesst i have given the wrong fieldname in my $sql statment. The error is hapenning but is not showing.
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;
}
}
}
?>
The test code
$conn = new Test;
$conn->runQuery("tch_phn", "UPDATE `employee` SET `uiS`='Assad' WHERE `uid`='Assad' ");
The error is i have given a unknown field name (uiS
has to be uid
). How can i do this?
回答1:
You don't need the Dbc
class. It is not useful to you at all at its current state. The mysqli connection is always the same three lines of code and there is no point to add a class for it.
To connect to DB using mysqli use:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db_name');
$mysqli->set_charset('utf8mb4'); // always set the charset
Then you can write a class which will take the DB connection as a parameter in __construct()
.
class Test {
private \mysqli $db = null;
public function __construct(\mysqli $db) {
$this->db = $db;
}
function runQuery($sql) {
$query = $this->db->query($sql);
}
}
That's it. Although you really should try to create a more useful mysqli abstraction class, or even better, use PDO instead. If you want to write mysqli wrapper class you can start with the following idea (adjust to your needs):
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;
}
}
Then you can execute any SQL statement with a simple one line even when using mysqli.
来源:https://stackoverflow.com/questions/62667486/how-to-return-mysqli-connect-error-over-2-function-in-2-classes-php