Hello fellow programmers,
I have made a function that looks up the database if a login already exists and then returns either true or false, here is the body of this function:
public static function loginExiste($login)
{
$cnx = new GestionBD("localhost", "fnak", "root", "");
$login = mysql_real_escape_string($login);
$ret = $cnx->execRequete("SELECT COUNT(*) FROM clients WHERE Client_Login = '".$login."'");
$col = mysql_fetch_array($ret);
echo 2;
if ($col[0] > 0)
{
echo 3;
return true;
}
else
{
echo 4;
return false;
}
}
and here is how I call this function:
echo 1;
if (!ExecRequete::loginExiste($_POST['login']))
{
echo 5;
/*
echo '<center><p style="color:red;">
Erreur: Login existe déjà
</p></center>';
exit();
*/
}
echo 6;
Now as you can see, I have some echo statements scattered around to see how the execution is flowing. The result I get every single time is this:
if the login exists: 123
if it doesn’t exist: 124
From the result I see that the script stops the execution right after the return statement. normally It should be like this:
1236 or 12456
The worst part about this is that it happened to me during an exam, that made me very upset as it doesn’t make sense at all. The debugging lost me so much time that I couldn’t finish the other easy parts..
Can anybody see why is this strange behavior happening here ?
Finally ! I got where the error was ^^
after restarting the function from scratch (like using if ($login == “a”) return true;) I found out that the problem was in the destructor of my GestionBD class that handles the connection with mysql. Because all the code in the function worked except at the very end on the return statement.
What made me sure it was the destructor, is when I put $cnx = null; before my echo 2; and the execution stopped before the echo. Then I commented out the destructor and everything worked as needed. but I wondered what was wrong in my destructor…
Here’s how it looked:
Now, if anyone is familiar with OOP in PHP, he will spot the error right away.. the variable connexion is a member variable in the class. So when we refer to it with the pointer $this we needed to put it without $ (ahh I hate you $, I either forget you or put when you’re not needed :/ )
the correct destructor is this:
but this mysql_close function is certainly not that well made, it doesn’t give any error message, not any exception, it just crashes the engine and leaves you wondering why ..
Thank you all for your answers and help, it’s really appreciated 🙂 and an advice for everyone is that if you get any strange behavior or error messages at a simple return true; check all you local object’s destructors.. they may be guilty, and also
USE PDOdon’t use@with mysql functions when debugging as it supresses any error output 😉