doushao1087 2014-04-30 02:51
浏览 156

affected_rows可能会返回0,1,-1,3或在某些情况下为NULL [建议的答案]

The PHP documentation contains the reasons why an affected_rows statement might return 0, 1, -1 or even 3 in some scenarios, as does the answer to this particular stack question.

PHP / MySQLI: strange readings for affected_rows

But the wall I hit was that affected_rows was returning NULL. I did not find any stack question which currently answers this issue. So I figured out why it would return NULL.

Its because you are making a mistake in your syntax. I tend to associate the usage of affected_rows to the same as num_rows. But in fact the usage of both is different and that is why I got the particular NULL value.

num_rows usage:

$query = $handler->query("some select query");
$rows = $query->num_rows;

this would return the proper number of rows;

affected_rows usage:

$query = $handler->query("some update insert query");
$rows = $query->affected_rows;

this would return null as the $query variable unlike the previous $query would return true or false. Therefore in such a case you would always have to make calls to the $handler database connector object.

This is a problem I dealt with just now and now when I go back and look at the docs page I see that I've totally missed it. I think many people tend to treat affected_rows the same way as num_rows, but that's just me. But I think even though the question is a self-answering one it deserves a place here, because it took me almost 12 hours before I finally did a var_dump and got to the core of the issue.

If someone knows a better answer to this then please improve this answer.

The question lies in why are both num_rows and affected_rows treated differently in such a context?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏