tags:

views:

249

answers:

6

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 = '[email protected]';
$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.

A: 

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

Jon
Even if i do $email = 'something' right above the block, i still get same results.
lyrae
Oh, looking at the error again, it sounds like the problem must be with $statement (and the fact that in production, it is boolean).I think $sql->prepare() returns false if an error occurred, so you can look into that. Probably a database connection issue.
Jon
A: 

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.

DisgruntledGoat
Even if i just have the email query block by itself on a page, it doesn't work. So the order of where it is compared to other code on a page doesn't make a difference. Thanks for the info about PDO prior to 5.3. I am running 5.2.6, so maybe this could be why. Although my current host is using 5.2.9 a believe, and it works fine. I will ask them to upgrade to 5.3 and see what happens. thanks!
lyrae
A: 

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

lyrae
+1  A: 

I'd better recommend using this:

$email = '[email protected]';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
$statement->execute(array($email));

No BindParam use is needed here.

FractalizeR
+1  A: 

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?

jmucchiello
Yep, I am. It's a signup script and i simply query to see if the username exists, and then i query again to see if email exists on the 'users' table.
lyrae
And they are all the using the same version of PHP? All the libraries are the same?
jmucchiello
A: 

Hi, 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

zoombaro