0

I want to learn how to display specific errors when my MySQL queries fail. For example, in the following code, I've deliberately written the table name wrong. So instead of displaying any error, the browser simply doesn't load the page. It displays the message The localhost page is not working.

To display error, I tried to use $mysqli->error, but it's not working. Same is when I change the database name to wrong database to get connection error. Is there something wrong?

Here's my code:

<?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);
}
?>
user3.14
  • 371
  • 2
  • 15

0 Answers0