Page 1 of 1

PHP 5 PDO: Weird Database Behavior

Posted: Thu Jun 29, 2006 6:20 pm
by santosj
Okay, I've done a few tests:
  • I've tested that $GLOBALS['mysql'] is a PDO resource and the Information is accurate.
  • I tested if I was getting any errors, and PDO wasn't receiving any from MYSQL.
  • I'm not getting any exceptions.
  • The foreach loop isn't returning anything. So I suppose that the table doesn't exist, but it does. I see it and the database has the table.

Code: Select all

if(!empty($_POST['username']) and preg_match('/^[a-zA-Z]{1}[ _a-zA-Z0-9-]{5,99}$/', $_POST['username']) and preg_match('/^[a-z-A-Z0-9]{5,100}$/', $_POST['password']))
{
    try {
        $statement = $GLOBALS['mysql']->prepare("SELECT COUNT(*) FROM gh_users WHERE username=:username AND pass=PASSWORD(:password)");
        $statement->bindParam(':username', $username);
        $statement->bindParam(':password', $password);
	
        $username = $_POST['username'];
        $password = $_POST['password'];
        $statement->execute();
	
        $row = $statement->fetch(PDO::FETCH_NUM);
	
        $exists = (bool) $row[0];
	
        foreach($GLOBALS['mysql']->query("SELECT * FROM gh_users") as $row)
        {
            print_r($row);
        }
    }
    catch(PDOException $e) { var_dump($e); }
}
I can't give you any Database Information, so I don't know how much help you can be, but if you have any tips for me to figure out to solve my problem then I'll be happy. I'm going to continue my tests to see where I screwed up on.

Posted: Thu Jun 29, 2006 7:54 pm
by bdlang
Are you certain the first if() statement that tests the values in $_POST return TRUE? Use echo() to display something past that point (prior to the try{} block) to verify your input validation.

Code: Select all

$exists = (bool) $row[0];
Does $row[0] contain a 0,1,TRUE, FALSE flag? What role does $exists play in the rest of the script? I see no conditional using this value.

Posted: Thu Jun 29, 2006 8:26 pm
by santosj
Um, yeah, the preg_match does work and the test code did print.

I also do use Exists later in the code. I removed it because it isn't part of the problem.

$exists evaluates to false if $row[0] is 0 or less than 0. It evalutes to true if it is 1 or above.

Posted: Thu Jun 29, 2006 8:47 pm
by bdlang
Hmm, ok how about adding this just below your $statement->execute() line:

Code: Select all

print_r($statement->errorInfo());
You might also try changing your exception handling, e.g.

Code: Select all

}
catch (PDOException $e) { echo $e->errorInfo; }
Looking over the PHP manual section on PDO, the PDOException class should trap errors from the instantiation / connection and any statements, but you might mix up your error handling a bit and see what jumps out.

Posted: Thu Jun 29, 2006 8:56 pm
by santosj

Code: Select all

print_r($statement->errorInfo());

Code: Select all

Array ( [0] => 00000 )
I think it may also have to do with creating two connections to the same database. One with mysqli and another with PDO. I was in the process of changing over to PDO, but came into some other problems with PDO statements and Queries.

The reason why I'm using PDO is because MySQLi doesn't work either. It doesn't give any errors, it just doesn't work. Much like PDO.

The normal page works, so I have no idea what the difference from this file and the other.

---------

Hell, I even tried debug_print_backtrace() which returned nothing at all, which I'm confused over.

Also, I thank you for helping.