-2

I've been reading a lot of posts here in StackOverFlow and the documentation info at php dot net.

Here's the code I'm tryng:

Example 1

$id = 1;
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = ?');
$sth->execute(array(intval($id)));

Example 2

$id = 1;
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = :id');
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->execute();

Example 3

$id = 1;
$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = :id');
$sth->bindValue(':id', intval($id));
$sth->execute();

If I try this:

$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = 1');
$sth->execute();

I get the result I'm expecting but it's not the solution I'm looking for.

Hope you can help me, thanks in advance.

////////// EDIT 1

At the end of all of this examples I'm doing this:

$arr = $sth->errorInfo();
print_r($arr);

The return is:

Array ( [0] => 00000 [1] => [2] => )

/////////// EDIT 2

Here's the code

class User {

    private $registry;
    private $userId;
    private $userLan;
    private $fullName;
    private $dob;
    private $email;
    private $sex;
    private $nationality;
    private $valid; // User valid?
    private $pdo; // PDO reference

    /**
     * Constructor del usuario. Se puede construir de dos formas, pasando email y password o pasando una id de usuario.
     * @param Registry $registry.
     * @param Int $id
     * @param String $email
     * @param String $password
     * @param String $username
     * @return void
     */
    public function __construct ( Registry $registry, $id, $email, $password, $username)
    {
            echo "constructor user with id = $id ";
            $this->valid = false;
        $this->registry = $registry;
        if($id = 0 && $username != '' && $password != '')
        {
                    // ...
                    $this->valid = true;
        }
        //else if($id > 0)
            else
        {
            // $id = intval($id);
                echo "second if";
                $this->pdo = $registry->getObject('db')->getPdo();
                try
                {
                    $sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = :id');
                    $sth->execute(array(':id' => intval($id)));
                    //$sth->execute(array(intval($id)));
                    $arr = $sth->errorInfo();
                    print_r($arr);
                    $result = $sth->fetch(PDO::FETCH_BOTH);
                    print_r($result);
                }
                catch(PDOException $e)
                {
                    echo $e->getMessage();
                }
                echo "passing query";
                if($sth->rowCount() ==1)
                    echo "yeeeha";
                else
                    echo "not yeeha (".$sth->rowCount().") ";
                    // ...
                    $this->valid = true;
        }
    }
Jesus
  • 1,116
  • 3
  • 10
  • 17

2 Answers2

0

Your error checking does not look correct:

$sth = $this->pdo->prepare('SELECT * FROM USERS_TABLE WHERE ID_USR = :id');
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->execute();
$arr = $sth->errorInfo();

This assumes that PDO::prepare() always returns an object, but that's not what documentation says:

Return Values

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

I suggest you make your life easier and configure PDO to throw exceptions. This example is taken from the manual:

$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                      ^^^^^^^^^^^^^^^^^^^^^^
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thanks, actually fixed it (@YourCommonSense gave me another solution for this above) +1 /// Anyway the code is not throwing exceptions. – Jesus Jul 25 '13 at 12:11
  • Well, that's the problem with "not working" as only explanation: any answer is just a blind shot. – Álvaro González Jul 25 '13 at 12:20
0

I hate to guess but the only sensible explanation I can find is a problem with $id and lack of error reporting

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345