1

I am trying to figure out why my PDO methods keep returning null values. Everything seems correct, from the prepare to the execute.

i.e. "Fatal error: Call to a member function prepare() on null in /blah/register.php on line 5"

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); if ($email === ''){ unset($email); }else{ $query = $mysqli->prepare('SELECT email FROM `Members` WHERE email=:input_email'); $query->execute(array(':input_email' => $email)); foreach ($query as $row) { echo $row['email'] . "<br />"; } var_dump($query); die(); } 

This code should echo all values, and then kill the rest of the script.

db connect:

$db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8', 'dbuser', 'password', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT)); if ($db->connect_errno) { echo "<b>Sorry, this website is experiencing problems. </b>"; exit; } 
3
  • 1
    You are mixing mysqli and pdo!! post your database connection code too Commented Dec 5, 2015 at 6:50
  • And with this query SELECT email FROM Members` WHERE email=:input_email` you always get $email Commented Dec 5, 2015 at 6:51
  • oh wow just realized its using mysqli. Commented Dec 5, 2015 at 6:52

1 Answer 1

1

You are mixing mysqli and pdo in your code you need to change mysqli prepare withpdo prepare`

 $query = $db->prepare('SELECT email FROM `Members` WHERE email=:input_email'); ^^^ //add pdo object here 

You need to add fetch data form your execute statement

 $query = $db->prepare('SELECT email FROM `Members` WHERE email=:input_email'); $query->execute(array(':input_email' => $email)); $result = $query->fetch(PDO::FETCH_ASSOC); print_r($result); 

Read http://php.net/manual/en/pdostatement.fetch.php

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This is exactly what I was trying to figure out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.