views:

608

answers:

4

Hi guys,

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
+4  A: 

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 the if statement with something like

if (isset($row))
  doSomething();

It'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.

Platinum Azure
`defined` doesn't do what you think here. You probably mean `isset`.
Alex Barrett
Damnation, that is precisely what I meant. Edit forthcoming in seconds.
Platinum Azure
+1  A: 

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.

print $row[2]

vs

print $row['email];
Anti Veeranna
cheers. but can't I do $row['email'] with mysql_fetch_array() as well can't I? I've just used $row[1] to for debugging purposes to make sure i haven't named the keys incorrectly or something
cosmicbdog
According to the mysql_fetch_array() page (http://us2.php.net/mysql_fetch_array), you can use either (the array has both numeric and associative indexing).Personally, I would still favor mysql_fetch_assoc() in case that reduces overhead by only allowing one form of indexing. But I don't know the ins and outs of PHP well enough to know if this is worthwhile or not.
Platinum Azure
+1  A: 

Use

print_r($row);

to see what the contents of the $row variable is.

Vegard Larsen
cheers Vegard. I did and have modified the original post to show what happens.
cosmicbdog
A: 

I have no idea what is going on.

I went through changed all the '$row' to '$rowElse' and am still getting undefined variable: row. There's no 'row' variable anywhere to be found. There's no included files with $row anywhere. Its completely mind boggling. As soon as I remove the code above the error disappears. It really doesn't make any sense.

cosmicbdog
Could you post the entire piece of code and the linenumber that PHP gives the warning about?
Lex
for some strange reason its not outputting a line number. normally it does...
cosmicbdog