PHP try/catch and fatal error

前端 未结 6 1858
庸人自扰
庸人自扰 2020-12-10 01:01

I\'m using the following script to use a database using PHP:

try{
    $db = new PDO(\'mysql:host=\'.$host.\';port=\'.$port.\';dbname=\'.$db, $user, $pass, $o         


        
6条回答
  •  生来不讨喜
    2020-12-10 01:57

    If database connection fails, $db from your first try .. catch block will be null. That's why later you cannot use a member of non-object, in your case $db->prepare(...). Before using this add

    if ($db) {
        // other try catch statement
    }
    

    This will ensure that you have db instance to work with it.

提交回复
热议问题