php更新mysql后获取影响的行数发生异常解决方法

yipeiwu_com6年前Mysql基础
从manual上知道了mysql_affected_rows函数当UPDATE前后的数据一样时会返回异常值,

下面有个方便的解决办法,从官方munual上看到 bdobrica at gmail dot com 留言的:
As a solution to the problem pointed in the post reffering to mysql_affected_rows() returning 0 when you are making an update query and the fields are not modified although the query is valid, i'm posting the following function. It is very simple and based on a previous post.
复制代码 代码如下:

function mysql_modified_rows () {
$info_str = mysql_info();
$a_rows = mysql_affected_rows();
ereg("Rows matched: ([0-9]*)", $info_str, $r_matched);
return ($a_rows < 1)?($r_matched[1]?$r_matched[1]:0):$a_rows;
}

PS:因为这个小问题折腾了半天,感觉php真是太不清晰了

相关文章

php写入mysql中文乱码的实例解决方法

php写入mysql出现中文乱码的解决办法是:在建立数据库连接之后,将该连接的编码方式改为中文。 代码如下: $linkID=@mysql_connect("localhost","...

PHP mysql与mysqli事务使用说明 分享

mysqli封装了诸如事务等一些高级操作,同时封装了DB操作过程中的很多可用的方法。 应用比较多的地方是 mysqli的事务。 比如下面的示例: 复制代码 代码如下: $mysqli =...

PHP商品秒杀问题解决方案实例详解【mysql与redis】

本文实例讲述了PHP商品秒杀问题解决方案。分享给大家供大家参考,具体如下: 引言 假设num是存储在数据库中的字段,保存了被秒杀产品的剩余数量。 if($num > 0){...

PHP实现基于mysqli的Model基类完整实例

本文实例讲述了PHP实现基于mysqli的Model基类。分享给大家供大家参考,具体如下: DB.class.php <?php //数据库连接类 class...

php+mysql开发中的经验与常识小结

本文总结了php+mysql开发中的经验与常识。分享给大家供大家参考,具体如下: 一、基础规范 (1)尽量使用 InnoDB 存储引擎 支持事务、行级锁、并发性能更好,CPU 及内存缓...