Undefined object + PDO

hey there,

I’m currently getting an undefined object fetchall error and PDO selection script. because from what I can see the PDO variable is being defined. But maybe that’s just me. There’s a good chance that I’m missing something.

In a config file. I have defined all variables needed and started the PDO connection as the variable $pdo.

and here’s the section of code that is giving me the grief



$queryselectemail = "SELECT COUNT(*) FROM users WHERE email = :email";

			$values =  array('email' => $email);

			try {
				$tryemail = $pdo->prepare($queryselectemail);
				$tryemail->execute($values);
			
			} catch (PDOException $e) {

				$title = "There was an error :(";
				$output = "We are sorry but there has been an error! The error is a PDO/Mysql error and it is {$e->getMessage()}";
			}
		}

			if($tryemail->fetchAll() < 1){
                                /* code to insert user data */
}

however I am just getting the error. I made if the e-mail already exists in the database. (Not shown above)

thanks in advance.

$values =  array(':email' => $email); 

I think … missing colon

Nope. I don’t need a colon in the array.

If i was to do it like

 $pdo->bindValue(':email', $email) 

you would need the colon. But done in a array you do not. OR at least from all the books i have read that is what i go out any ways.

oops… sorry.

When you say only getting the error, do you mean you are getting an exception?

Have you included the config file?

No problem man!

No it’s an “Email allready in use” ‘error’. Yes the config file is being included as up to that part is executing just fine.

Okay, I think this is a workflow issue, you will always have 1 row returned by your query because you are doing a COUNT(*). For a non-matching e-mail, the row will have a value of 0, for a matching e-mail, it will have a value of 1 (either way, you get 1 row returned with 1 value).

The second issue I see, is your fetchAll is assuming it returns an int value, it returns an array, so you might have to use $tryemail->fetchAll()[0][0] (signifies 1st row and 1st column. or if PHP doesn’t support that syntax the following may be the correct approach)


$rows = $tryemail->fetchAll();
if ($rows[0][0] < 1)
{
   // insert user data

Hey,

for got to post. But i got it resolved. What i eneded up doing was if what was retured was more then one i ened saying the email was in use. then in the else state that is where i insert the data.

Thanks for the help though