php mysqli 字段缺失,mysqli 为什么不提示字段异常

本文探讨了在使用mysqli进行数据库操作时,如何在发生错误,特别是字段错误时,让PHP程序正确抛出并捕获错误信息。通过示例代码展示了面向对象和面向过程两种风格的错误处理方式,确保在字段不存在的情况下执行更新操作时能获取到错误提示。
摘要由CSDN通过智能技术生成

mysqli 为什么不提示字段错误

本帖最后由 goimt 于 2015-08-29 21:48:01 编辑

注意是用mysqli,不是mysql (mysql是有提示的)

如:

query("update {$tpre}member set ttid='2000'  where userid123='10000'");

没有userid123这个字段,执行时,没有更新但也没有提示错误

怎么能让它提示 没有这个字段的 错误

用MYSQL5.7,php5.6

------解决思路----------------------

面向对象风格的错误抛出:<?php

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */

if ($mysqli->connect_errno) {

printf("Connect failed: %s\n", $mysqli->connect_error); //mysql连接错误抛出

exit();

}

if (!$mysqli->query("SET a=1")) {

printf("Errormessage: %s\n", $mysqli->error); //mysql查询错误抛出

}

?>

面向过程风格的错误抛出:<?php

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */

if (mysqli_connect_errno()) {

printf("Connect failed: %s\n", mysqli_connect_error());

exit();

}

if (!mysqli_query($link, "SET a=1")) {

printf("Errormessage: %s\n", mysqli_error($link));

}

?>

------解决思路----------------------

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */

if (mysqli_connect_errno()) {

printf("Connect failed: %s\n", mysqli_connect_error());

exit();

}

//  $mysqli->affected_rows 取得前一次 MySQLI 操作所影响的记录行数

$mysqli->query("update {$tpre}member set ttid='2000'  where userid123='10000'");

printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);

$mysqli->affected_rows 在mysqli可以取得前一次 MySQLI 操作所影响的记录行数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值