PHP 5 PDO: Weird Database Behavior

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

PHP 5 PDO: Weird Database Behavior

Post 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.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post 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.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

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