Question

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);

The following gives: Fatal error: Call to a member function bindParam() on a 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 strange.

At my local machine, and my remote host, this was never a problem.

This errors only shows up on this new hosting company I am trying out for the month. Could it be a config param when they compiled php?

--------edit-------- While still trying to figure out what's wrong,I found this out.

$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 a 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
        )

)

Which is correct. Says 4 rows returned and displays the result array. On another server however, 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
        )

)

Thus, it seems also that PDOStatement::rowCount() does not work on a sever but works on another.

Was it helpful?

Solution 2

Found a solution to the problem.

This is the entire 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 have to create a new instance of PDO. what's strange is that on 2 other servers, I don't have to do this.

And upon further looking, i found that PDO::Prepare raises an PDOExeption.

Here it 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 it'll help someone in the future :)

OTHER TIPS

Read this: $statement->closeCursor()

PDOStatement::closeCursor() frees up the connection to the server so that other SQL statements may be issued

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

Is $email undefined? You could try a var_dump($email) too see what it says. Good luck.

Have you tried putting the $email= line below the bindParam (but before you do the execute)? bindParam passes the parameter by reference so you can execute as query, change the value of the variable and execute again.

I think though that it's probably a PHP set up error. I've heard people saying PDO had a lot of bugs before PHP 5.3, so maybe see if you can get PHP up to the latest version?

Have you also tried swapping the two queries around? Maybe something breaks after you run one query.

I'd better recommend using this:

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

No BindParam use is needed here.

I had same problem with echo "Rows returned: " . $statement->rowCount();

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top