栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MySQLi准备的语句错误报告[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MySQLi准备的语句错误报告[重复]

我在过去两天里写了两次(对我来说,这是重复的,即使问题开始有所不同)。

mysqli的每种方法都可能失败。您应该测试每个返回值。如果失败了,请考虑继续使用不在您期望的状态的对象是否有意义。(可能不是处于“安全”状态,但是我认为这不是问题。)

因为只有在最后操作的错误信息被存储在每个连接/声明可能会丢失有关的信息 是什么
,如果你继续后出了问题造成的错误。您可能希望使用该信息来让脚本决定是重试(仅是临时问题),更改某些内容还是完全纾困(并报告错误)。而且它使调试变得容易得多。

$stmt = $mysqli->prepare("INSERT INTO testtable VALUES (?,?,?)");// prepare() can fail because of syntax errors, missing privileges, ....if ( false===$stmt ) {  // and since all the following operations need a valid/ready statement object  // it doesn't make sense to go on  // you might want to use a more sophisticated mechanism than die()  // but's it's only an example  die('prepare() failed: ' . htmlspecialchars($mysqli->error));}$rc = $stmt->bind_param('iii', $x, $y, $z);// bind_param() can fail because the number of parameter doesn't match the placeholders in the statement// or there's a type conflict(?), or ....if ( false===$rc ) {  // again execute() is useless if you can't bind the parameters. Bail out somehow.  die('bind_param() failed: ' . htmlspecialchars($stmt->error));}$rc = $stmt->execute();// execute() can fail for various reasons. And may it be as stupid as someone tripping over the network cable// 2006 "server gone away" is always an optionif ( false===$rc ) {  die('execute() failed: ' . htmlspecialchars($stmt->error));}$stmt->close();

编辑:六年后的几点笔记。…
mysqli扩展完全能够报告通过异常导致除0以外的(mysqli)错误代码的操作,请参见mysqli_driver :: $$
port_mode

die()确实非常粗糙,即使在这样的示例中,我也不会使用它。
因此,请仅考虑以下事实: 每个 (mysql)操作 都可能 由于多种原因而失败;甚至 ,如果 同样的事情进行得很顺利前一千倍....



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号