Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP undefined variable weird

Tags:

variables

php

Here's my script it verifies wether a username has been taken.

while ($row = mysql_fetch_array($result))
{
  $usname=$row['Username'];
}
if ($usname!=$uname)
{

} else {
  echo "Username taken!";
  die;
}

It works well. If a username is taken, it does not add it to the database, and will if it is unclaimed. But I always get this annoying error:

Notice: Undefined variable: usname in C:\xampp\htdocs\insert.php on line 29

I defined that variable!

Help...

like image 825
user1797443 Avatar asked Nov 04 '12 02:11

user1797443


3 Answers

If mysql_fetch_array() returns null your while-loop will never launch, thus $usname will never be initialized.

Try declaring it on the line above, like this:

$usname = null;
while($row = mysql_fetch_array($result))
{
   $usname=$row['Username'];
}
If ($usname!=$uname)
{

}else{
   echo "Username taken!";
   die;
}
like image 162
d_inevitable Avatar answered Nov 17 '22 21:11

d_inevitable


You have not declared $usname as a variable. Try putting $usname=''; before the while loop

like image 32
Jess McKenzie Avatar answered Nov 17 '22 22:11

Jess McKenzie


try using

mysql_fetch_array($result, MYSQL_ASSOC)
like image 1
lje Avatar answered Nov 17 '22 22:11

lje