PDO acts differently on two identical requests

The following block of code works fine (no errors)

$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- This returns 'object'
$statement->bindParam(1, $username);

      

Following are: Fatal error: Calling member function bindParam () on non-object in /file.php on line 39

$email = 'fake@email.com';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- this returns 'boolean'
$statement->bindParam(1, $email); // -- this is line 39.

      

Now this is weird.

On my local machine and my remote host, this has never been a problem.

These errors only show up on this new hosting company that I tried for a month. Can it be a config parameter when compiling php?

-------- edit -------- Although I'm still trying to figure out what happened, I found this.

$query = "select userID, username from users";
$statement = $sql->prepare($query);    
$statement->execute();
$r = $statement->fetchAll(PDO::FETCH_ASSOC);

// display # of rows
echo "Rows returned: " . $statement->rowCount();

// display results array
echo '<pre>'; print_r($r); echo '</pre>'; 

      

On the server, I get

Rows returned: 4

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

      

It is right. Speaks 4 lines and displays an array of results. However, on another server, I get

Rows returned: 0

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

      

So it looks like PDOStatement :: rowCount () doesn't work on the server, but it does on the other.

+2


source to share


6 answers


Found a solution to the problem.

This is the whole block of code.

// check if username exists
$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
$statement->bindParam(1, $username);
$statement->execute();


// check if email exists
$sql2 = new PDO('mysql:host=localhost; dbname=db', 'username', 'pw');
$query = "select * from users";
$statement = $sql2->prepare($query);
echo gettype($statement);
#$statement->bindParam(1, $email);

      

So for some reason I need to create a new PDO instance. what is strange is that on 2 other servers I don't need to do this.



And after further searching, I found that PDO :: Prepare calls PDOExeption.

Here he is:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

Array
(
    [0] => HY000
    [1] => 2014
    [2] => Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
)

      

Maybe this will help someone in the future :)

+1


source


Read this: $ statement-> closeCursor ()

PDOStatement :: closeCursor () releases the connection to the server so other SQL statements can be issued



Are you using the same database on the server where you say you don't have this problem?

+2


source


Is $ email undefined? You can try var_dump ($ email) to see what it says. Good luck.

0


source


Have you tried putting the line $email=

below bindParam

(but before executing)? bindParam

passes the parameter by reference, so you can execute the request, change the value of the variable, and execute it again.

I think this is probably a PHP bug. I heard people, PDO had a lot of bugs before PHP 5.3, maybe see if you can get PHP up to the latest version?

Have you tried replacing two questions as well? Maybe something breaks after running one request.

0


source


I would recommend using this:

$email = 'fake@email.com';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
$statement->execute(array($email));

      

The use of BindParam is not required here.

0


source


I had the same problem with echo "Returned lines:". $ Operator-> ROWCOUNT ();

I got -1 lines, lol. My INFORMIX database and Internet search I found that rowCount (); returns only the affected rows in a DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

With the SELECT statement you need to use the $ Operator-> fetchColumn () function;

read here: http://www.phpbuilder.com/manual/en/function.pdostatement-rowcount.php

0


source







All Articles