user1797443
user1797443

Reputation: 246

PHP undefined variable weird

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

Upvotes: 5

Views: 164

Answers (4)

user2059619
user2059619

Reputation: 884

You need to define $usname

$usname = null;

Upvotes: 0

lje
lje

Reputation: 170

try using

mysql_fetch_array($result, MYSQL_ASSOC)

Upvotes: 1

Jess McKenzie
Jess McKenzie

Reputation: 8385

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

Upvotes: 2

d_inevitable
d_inevitable

Reputation: 4461

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;
}

Upvotes: 3

Related Questions