php - MYSQL 错误在类函数中未正确显示
问题描述
$conn = new mysqli($host,$user,$pass,$dbname);
$sql = "INSERT INTO user (email,password) VALUES ('user@example.com','asdfasdfasdfsadf')";
if($conn->query($sql) != true){
echo $conn->error;
// Its display correct result, Duplicate entry 'user@example.com' for key 'email'
}
这里 $conn->error; 显示正确的结果,但在下面的课程中它没有显示任何内容
但问题是
<?php
class db
{
private $host;
private $user;
private $pass;
private $dbname;
}
class conn extends db
{
public function connect()
{
$this->host = "localhost";
$this->user = "root";
$this->pass = "";
$this->dbname = "jobportal";
$conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
return $conn;
}
}
class work extends conn
{
public function insert_user($email, $password_hash)
{
$sql = "INSERT INTO user (email,password) VALUES ('user@example.com','asdfasdf')";
if ($this->connect()->query($sql) == TRUE) {
echo 'inserted';
} else {
return $this->connect()->error;
// Return Blank No Result it returns, As its duplicated value it it will insert , so it will show error as duplicate.
}
}
}
$obj = new work;
echo $obj->insert_user('user@example.com', 'asdfasdf');
在这里,它没有显示任何内容,只是空白;有什么问题可以帮我吗?
解决方案
每次调用时,类中的connect()
方法都会创建一个新连接。conn
在您insert_user()
调用的方法connect()
中运行查询,然后再次调用它以查找错误。您用于查找错误的新连接与用于运行查询的连接是分开的,因此不会发现任何错误。
此外,为每个新查询创建一个新连接而不关闭它们可能会耗尽连接限制。您应该创建一个连接并每次都重新使用它。
以下是您的代码的修订版本。笔记:
- MySQLi 设置为在遇到错误时抛出异常。这些异常被捕获并报告在
try...catch
块中。 - 我已更改您的代码以使用准备好的语句来避免 SQL 注入。
- 您的数据库参数被传递到
Work
构造函数
class Conn // No parent db class - it serves no purpose
// Class names begin with capitals, by convention
{
private $host; // Local copies of database credentials. Not really needed
private $user;
private $pass;
private $dbname;
// Set up the database connection in the constructor.
public function __construct(string $host, string $user, string $password, string $dbname) {
$this->host = $host;
$this->user = $user;
$this->pass = $password;
$this->dbname = $dbname;
// Set mysqli to throw exceptions on error
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// open a connection for later use
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
}
}
class Work extends Conn
{
public function insert_user(string $email, string $password_hash)
{
$sql = "INSERT INTO user (email,password) VALUES (?,?)";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param('ss', $email, $password_hash);
$stmt->execute();
return;
}
}
try {
$obj = new Work("server", "username", "password", "schema");
$obj->insert_user('user@example.com', 'asdfasdf');
echo "inserted";
} catch (Exception $e) {
echo $e->getMessage();
}
推荐阅读
- asp.net - 在 Visual Studio 2019 Enterprise 中检查 Web 应用程序中未使用的代码
- awk - 相对于下一列转置一列
- azure - Azure API Management 提供的 API 网关和 Azure 应用程序网关有什么区别?
- azure-devops - 在 azure devops analytics api 中获取没有任何任务的用户故事计数
- java - Insert additional into a String
- recursion - 使用可变引用对 app_state 进行 Rust 递归更新
- c# - 如何将移动应用程序连接到 AD B2C
- android - 创建用于二维码的动态网址,将在所有平台(网站、Android 应用程序、IOS 应用程序)中使用
- javascript - 页脚不要放在页面底部
- python - Pandas 文本列根据唯一 ID 分组