3

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 ?

10
  • 6
    I can't see anything obviously wrong with your logic, but it's possible PHP is emiting an error that isn't being displayed. Is error_reporting and display_errors/log_errors configured? BTW, bit of advice regarding exam technique. If you find you're bogging down on something, leave it and come back to it later. Provided you get answers down they'll be marked, it doesn't matter if they're not in order. Commented Jan 28, 2012 at 18:17
  • yeh I already added the instructions: error_reporting(E_ALL); ini_set('display_errors', true); in the functions script and in the main one. I also took the function out of the class and put it in the same script, and I tried using a global variable to output the value but the execution never goes beyond the function.. Commented Jan 29, 2012 at 16:25
  • 2
    There's always xdebug or zend debugger to track down spurious issues like this. Commented Jan 29, 2012 at 18:02
  • 1
    Why dont you try using try/catch block like this try{ //supicious code } catch (Exception $e) { $e->getMessage(); } Commented Jan 30, 2012 at 15:10
  • well I already tried the Try/catch block around the function.. didn't do the trick.. what's left for me is to use xdebug, but I need to install it and know how to use it with netbeans.. Commented Jan 30, 2012 at 18:33

1 Answer 1

2

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:

function __destruct()
{
    @mysql_close($this->$connexion);
}

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:

function __destruct()
{
    @mysql_close($this->connexion);
}

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 PDO don't use @ with mysql functions when debugging as it supresses any error output ;)

Sign up to request clarification or add additional context in comments.

1 Comment

you may want to remove @ on mysql_close so that it will not suppress the error specially during debugging or development. upvote on the answer downvote on the question as the question does not lead to the answer. The error is found on GestionBD and not in the code provided

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.