Sebastian
Sebastian

Reputation: 1654

PDO won't give results

So, I've switched from mysql_* to PDO today and am trying to get it running, but somehow I get zero results.

My Statement is as follows:

$serverConnector = new ServerConnector();
    $db = $serverConnector->connectToServer();
    $stmt = $db->prepare("SELECT * FROM `" . TABLE_USERS . "` WHERE `" . USER_NICKNAME . "` =:user_nickname OR `" . USER_EMAIL . "` =:user_email;");
    $stmt->execute(array(':user_nickname' => "'".$name_or_email."'", ':user_email' => "'".$name_or_email."'"));
    while ($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
        $this->id = $row[USER_ID];
        $this->joined = $row[USER_JOINED];
        ...
    }
    $db = null;

I've tried executing this statement as pure SQL on Xampp. Worked there. Doesn't work here though. I've tripple checked all parameters im passing and everything is correct. Somehow the program won't enter the while loop at all. ServerConnector simply connects to the Database via PDO and returns an PDO Database Object to $db.

Thanks

EDIT: Well, I'm using the newest Xampp with PHP 5.4.7. PDO Should be enabled by default. Checked the php.ini file myself and it was enabled in there as well

EDIT: Here's my ServerConnector class. Errorreporting enabled. Though I won't get any :/

class ServerConnector {

    public function connectToServer(){
        $db = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=UTF-8', 
                       DATABASE_USERNAME, 
                       DATABASE_PASSWORD, 
                       array(PDO::ATTR_EMULATE_PREPARES => false,
                             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
        return $db;
    }

}

Upvotes: 0

Views: 179

Answers (4)

Sebastian
Sebastian

Reputation: 1654

Alright, checked my DB output with print_r($row) and found out I'm having a two dimensional array here that my program doesn't go through. Means I'm having all my info on $row[0][KEY] and not on $row[key].

Fixed :)

EDIT: Well, I'll also post my code. Had it the exact same way two hours ago, but it didn't work. Don't ask me why -.-'Works like this:

function receiveUserData( $name_or_email ) {

    $serverConnector = new ServerConnector();
    $db = $serverConnector->connectToServer();
    $stmt = $db->prepare("SELECT * FROM `" . TABLE_USERS . "` WHERE `" . USER_NICKNAME . "` =:user_nickname OR `" . USER_EMAIL . "` =:user_email");
    $stmt->execute(array(':user_nickname' => $name_or_email, ':user_email' => $name_or_email));

    $row = $stmt->fetch(PDO::FETCH_ASSOC); 

    $this->user_id = $row[USER_ID];
    $this->user_joined = $row[USER_JOINED];
    $this->user_last_seen = $row[USER_LAST_SEEN];
    $this->user_salt = $row[USER_SALT];
    $this->user_nickname = $row[USER_NICKNAME];
    $this->user_name = $row[USER_NAME];
    $this->user_last_name = $row[USER_LAST_NAME];
    $this->user_email = $row[USER_EMAIL];
    $this->user_password = $row[USER_PASSWORD];
    $this->user_save_password = $row[USER_SAVE_PASSWORD];

    $db = null;
}

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157828

Your question lacks critical thing: an error message

It seems that the following is the most demanded answer to PDO questions:

Your real problem is lack of error reporting. So

error_reporting(E_ALL);

to be notified of all PHP errors like misspelled constants

$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

in your server connector to be notified of PDO errors

After getting the actual error message you can either solve it yourself or ask for assistance here

Upvotes: 0

Green Black
Green Black

Reputation: 5084

You dont have to quote with a prepared statement:

$serverConnector = new ServerConnector();
    $db = $serverConnector->connectToServer();
    $stmt = $db->prepare("SELECT * FROM `" . TABLE_USERS . "` WHERE `" . USER_NICKNAME . "` =:user_nickname OR `" . USER_EMAIL . "` =:user_email;");
    $stmt->execute(array(':user_nickname' => $name_or_email, ':user_email' => $name_or_email));
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $this->id = $row[USER_ID];
        $this->joined = $row[USER_JOINED];
        ...
    }
    $db = null;

Upvotes: 4

crush
crush

Reputation: 17013

Building on what was said above about not needing to add quotations in the execute():

$serverConnector = new ServerConnector();
$db = $serverConnector->connectToServer();
$stmt = $db->prepare("SELECT * FROM `" . TABLE_USERS . "` WHERE `" . USER_NICKNAME . "` =:user_nickname OR `" . USER_EMAIL . "` =:user_email;");
$stmt->execute(array(':user_nickname' => $name_or_email, ':user_email' => $name_or_email));
while ($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
    $this->id = $row[USER_ID];
    $this->joined = $row[USER_JOINED];
    ...
}
$db = null;

The following doesn't need to be in a while loop:

$row = $stmt->fetchAll()

This doesn't need to be in a while loop. It returns the entire recordset as an array, so you will only get a single iteration out of that loop.

You can either change that to $stmt->fetch(PDO::FETCH_ASSOC) or not use the while loop, and iterate over $row.

Upvotes: 2

Related Questions