Home  >  Article  >  Backend Development  >  php怎么让错误语句不报错

php怎么让错误语句不报错

PHPz
PHPzOriginal
2016-06-06 20:29:312126browse

php让错误语句不报错的设置方法:首先在语句前加上“@”符号,其语法是“@mysql_query($sql, $handle);”;然后用“error_reporting”调节报错等级即可。

php怎么让错误语句不报错

php怎么让错误语句不报错?

问题:

比如某条sql插入语句如果异常,就让他略过这句继续执行下面的,而不是抛个异常中断运行。像python的try一样

方法:

如果你用PDO,可以try...catch

try {
    $pdo->exec($sql);
} catch (PDOException $ex) {
    echo $ex->getMessage();
}

如果你用的mysql或者mysqli函数,可以在语句前加上@符号,比如

@mysql_query($sql, $handle);

这样可以关闭单行语句的错误输出

如果只是警告的话,可以用error_reporting调节报错等级

更多PHP相关知识,请访问PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn