Welcome Guest, Not a member yet? Register   Sign In
Quick MySQL validation question
#1

[eluser]EdgeTube[/eluser]
Hi there,

Quick question; probably doing something wrong that I'm missing.
Here's the code that I have:

Code:
$query = $this->db->query("SELECT username FROM user_table WHERE (username='$name' OR username='$lowercasename')");
  
     if ($row->username = $name)
     {
foreach ($query->result() as $row)
{
     $profileid = $row->username;
?>
<?="$profileid";?> exists.
<?  }} else {  ?>
    echo "user doesn't exist";
    <?  }
   }
?>

Yeah my indentation is terrible but I'll format that haha.

This is supposed to check the passed data['name'] and check that against the database to see if the user exists, and as you can see above, it echos when a user exists or not.

However, the 'else' part of the function doesn't seem to be working or displaying anything...

Thanks!
#2

[eluser]kgill[/eluser]
Ok first this isn't anything to do with MySQL it's a logic problem, 2nd lose the short tags, you seriously added all those open and close tags just to avoid typing one echo? Finally if you'd fixed your indenting the problem might have jumped out at you.

Code:
$query = $this->db->query("SELECT username FROM user_table WHERE (username='$name' OR username='$lowercasename')");
  
if ($row->username = $name) {
    foreach ($query->result() as $row)
    {
        $profileid = $row->username;
        echo $profileid ." exists.";
    }
} else {
    echo "user doesn't exist";
}
}
?>

I'm seeing an extra } but that may be because you didn't post your entire code. However the bigger problem here is you've got an if statement testing for $row->username before you actually fetch the row in the foreach, needs to be the other way around.




Theme © iAndrew 2016 - Forum software by © MyBB