ensure that PDO fetch method result “false” is a error or empty result

后端 未结 1 1720
Happy的楠姐
Happy的楠姐 2020-12-21 15:59

How can I recognize a fetch error or empty result from a execute error with php PDO mysql driver?

  $sql = \' SELECT * \';
  $sql .= \' FROM test \';
  $sql          


        
相关标签:
1条回答
  • 2020-12-21 16:34

    If there's an error then PDO will throw an exception which you can catch. Wrap your code with a try catch and that should catch any exceptions.

    try {
        $rs = $db->prepare('SELECT * FROM foo');
        $rs->execute();
        $foo = $rs->fetchAll();
    } catch (Exception $e) {
        echo 'Error: '.$e->getMessage();
    }
    
    0 讨论(0)
提交回复
热议问题