我得到“未定义的变量” PHP通知
我很好奇为什么我之前已经做过一百万次的事情会出现错误,但突然在某个脚本上出现错误“未定义的变量:行”,
但行对我来说似乎已定义。上面
$sql = 'SELECT * FROM table WHERE id="1" LIMIT 1 ';
$res = mysql_query($sql);
if(mysql_num_rows($res) != FALSE) {
$row = mysql_fetch_array($res);
}
是伪sql...但我检查了该sql 语句,我知道它会产生结果。我还知道 $row 正在存储数据,因为如果我去,
echo $row[0];
我会得到正确的数据。
据我所知, $row 变量已定义。然而仍然是一个错误。我是失去了理智还是我在这里错过了什么?难道这个错误/通知不应该只在 $row 不存在时发生吗?
编辑
抱歉,这一切都发生在 if 语句内:
$sql = 'SELECT * FROM table WHERE uID="' . $ID . '" LIMIT 1 ';
$res = mysql_query($sql);
if(mysql_num_rows($res) != FALSE) {
$row = mysql_fetch_array($res);
$firstName = $row[0];
$lastName = $row[1];
$email = $row[2];
}
编辑 2
如果我执行 print_r($row) 我得到以下内容:
Array
(
[0] => Robert
[firstName] => Robert
[1] => Nibbles
[lastName] => Nibbles
[2] => [email protected]
[email] => [email protected]
)
Undefined variable: row
I'm curious as to why I'm getting an error on something I've done a million times before but am all of a sudden on a certain script getting an error 'Undefined variable: row'
Yet row seems defined to me...
$sql = 'SELECT * FROM table WHERE id="1" LIMIT 1 ';
$res = mysql_query($sql);
if(mysql_num_rows($res) != FALSE) {
$row = mysql_fetch_array($res);
}
The above is pseudo sql... but I've checked that sql statement and I know its bringing out a result. I also know that $row is storing the data because if I go
echo $row[0];
I get the right data.
So to my knowledge, the $row variable is defined. Yet still - an error. Am I losing my mind or what am I missing here? Shouldn't this error/notice only occur if $row didn't exist?
edit
Sorry guys its all happening INSIDE the if statement:
$sql = 'SELECT * FROM table WHERE uID="' . $ID . '" LIMIT 1 ';
$res = mysql_query($sql);
if(mysql_num_rows($res) != FALSE) {
$row = mysql_fetch_array($res);
$firstName = $row[0];
$lastName = $row[1];
$email = $row[2];
}
edit 2
if i do a print_r($row) I get the following:
Array
(
[0] => Robert
[firstName] => Robert
[1] => Nibbles
[lastName] => Nibbles
[2] => [email protected]
[email] => [email protected]
)
Undefined variable: row
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有将
$row
初始化为 if 语句之外的内容,则它是未定义的。否则,如果您不想将
$row
初始化为某种 null 值(并非完全不合理),您可能需要将任何检查$row
的代码包含在外部if
语句的类似It's a pain, but you've just必须记住,任何未显式定义的变量,即使是 null,都是未定义的,并且可能会导致运行时错误,如果在代码中作为右值引用(除了
isset
等)。所以一般来说,要么总是初始化变量,要么自由地应用上面的代码。如果这不是问题,我深表歉意,但在没有看到您的代码的情况下,我无法想到更多的事情。
编辑:抱歉,这是“isset”而不是“define”。自从我真正使用 PHP 以来已经有一段时间了。我试图用概念而不是语法来回答这个问题。我的错误。
If you don't initialize
$row
to something outside that if statement, then it's undefined.Otherwise, if you don't want to initialize
$row
to some kind of null value (not entirely unreasonable), you might want to surround any code that checks$row
outside of theif
statement with something likeIt's a pain, but you've just got to remember that any variables you don't define explicitly, even to null, are undefined and can lead to a runtime error if referenced as an rvalue in code (except in
isset
etc.). So in general, either always initialize your variables or liberally apply code like the above.I apologize if this turns out not to be the issue, but I can't think of anything more than this without seeing your code.
EDIT: Sorry, it's "isset" not "defined". Been a while since I've actualy worked with PHP. I tried to answer the question with a concept, not syntax. My mistake.
题外话,但我建议使用 mysql_fetch_assoc() 而不是 mysql_fetch_array,然后你可以使用实际的字段名称您的代码,而不是一些任意数字。
与
Offtopic, but I recommend using mysql_fetch_assoc() instead of mysql_fetch_array, then you can use the actual field names in your code, instead of some arbitrary numbers.
vs