我想学习在MySQL查询失败时如何显示特定错误。例如,在以下代码中,我故意将表名称写错了。因此,浏览器不会显示任何错误,也不会加载页面。它显示消息本地主机页面无效。
要显示错误,我尝试使用$mysqli->error
,但它无效。当我将数据库名称更改为错误的数据库以获取连接错误时也是如此。有什么不对吗?
这是我的代码:
<?php
class Database
{
private $servername;
private $username;
private $password;
private $dbname;
public $abc;
public $con;
public function __construct()
{
$this->servername = 'localhost';
$this->username = 'root';
$this->password = '';
$this->dbname = 'dbind';
$this->con = mysqli_connect($this->servername, $this->username, $this->password, $this->dbname);
if(!$this->con){
echo $this->con->error;
}
return $con;
}
public function insert_data($emp_name, $emp_dept, $emp_salary)
{
$this->abc=$this->con->prepare("INSERT INTO tablec1(name, email, password) VALUES(?, ?, ?)");
$this->abc->bind_param("ssi", $emp_name, $emp_dept, $emp_salary);
$this->abc->execute();
}
}
?>
<?php
$conobj=new Database();
$query=$conobj->insert_data("vvsdf", "vsomeone", 3445);
if(!conobj->abc){
die($conobj->con->error);
}
?>