douqixia7942 2016-10-24 07:26
浏览 100
已采纳

为什么这个PHP connect_error无法正常工作?

Main question:

I'm trying to print error on web page if the connection to MySQL server is not made. For this, I written a wrong database name $this->dbname = 'dbinds'; (actual is dbind). But the die statement is not executing. When I open the page, it simply doesn't load. It shows error: The localhost page isn’t working. Am I doing something wrong?

Another question: I've commented the exception handling code block. Now when I use it, it works fine. The exception is thrown if there's some mistake. But if you see, I've written the 2nd exception throw code block below $this->abc=$this->con->prepare(); statement. But it doesn't work if I write this below $this->abc->execute();

Is there any way to throw exceptions for each statement? (Like, prepare, bind and execute).

Here's my code:

<?php
class Database
{
    private $servername;
    private $username;
    private $password;
    private $dbname;
    public $abc;
    public $con;
    public function __construct()
    {
       // ini_set('display_errors',1); error_reporting(E_ALL);
        $this->servername = 'localhost';
        $this->username = 'root';
        $this->password = '';
        $this->dbname = 'dbinds';
        $this->con = mysqli_connect($this->servername, $this->username, $this->password, $this->dbname);
        if($this->con->connect_error){
            die("Something: ".connect_error());
        }
        /*if(!$this->con) {
           throw new Exception("Connection can't be made");
        }*/
        return $this->con;
    }

    public function insert_data($emp_name, $emp_dept, $emp_salary)
    {
        $this->abc=$this->con->prepare("INSERT INTO table1(name, email, password) VALUES(?, ?, ?)");
        //FOLLOWING IF BLOCK
        /*if(!$this->abc) {
            throw new Exception("Couldn't insert data");
        }*/
        $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);
/*try{
    $conobj=new Database();
    $query=$conobj->insert_data("vvsdf", "vsomeone", 3445);

} catch (Exception $e ) {
    //echo "Service unavailable";
    echo "message: " . $e->getMessage();   // not in live code obviously...
   // echo $e;
    exit;
}*/
?>
  • 写回答

1条回答 默认 最新

  • douxian1770 2016-10-24 07:29
    关注

    You need to decide whether you use procedular or object oriented version of mysqli.

    However, still you can change your if condtion to:

    if (!$this->con) {
       die(mysqli_connect_error());
    }
    

    it should work. You can also see the example on manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计