3

I am trying to check if email was already used in Registration. It worked well when I was working on it in the school but now it suddenly shows an error:

Fatal error: Call to a member function prepare() on null

I use this to include

define("dbserver", "localhost");
define("dbuser", "user");
define("dbpass", "");
define("dbname", "user");    


$db = new PDO(
"mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
) );

In here

session_start();
include "DBjoin.php";
if(isset($_POST["email"])) {
  $_SESSION['email'] = $_POST["email"];
  }   
if(isset($_POST["nick"])) {
  $_SESSION['nick'] = $_POST["nick"];         
}    
if(isset($_POST["pass"])) {
  $_SESSION['pass'] = $_POST["pass"];
  $_SESSION['pass'] = base64_encode($_SESSION['pass']);    
}
$sthandler = $db->prepare("SELECT Email FROM Registrace WHERE Email =     :email");
$sthandler->bindParam(':email', $_SESSION['email']);
$sthandler->execute();               
if(filter_var($_SESSION['email'], FILTER_VALIDATE_EMAIL)) {
if($sthandler->rowCount() > 0){
      echo "Email is used";}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Mark
  • 75
  • 1
  • 1
  • 8
  • Store $db in a global variable, try it – ka_lin Jun 03 '15 at 17:39
  • 3
    @KA_lin why not just through their function instead? global is often frowned upon. Plus, it's not used in a function, so no use for it. – Funk Forty Niner Jun 03 '15 at 17:39
  • Connection failed. Look for error messages – Hanky Panky Jun 03 '15 at 17:40
  • Add `$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened. Plus, add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Jun 03 '15 at 17:44
  • *"Call to a member function prepare() on null"* - this tells me that your query failed. Make sure your HTML form is using a POST method and that your inputs all hold the `name` attribute with no typos etc. Include your HTML form in your question to leave the guesswork out of it. Also check your table and column names; there might be a letter-case issue also. On certain systems, `Email` and `email` are not considered the same. – Funk Forty Niner Jun 03 '15 at 17:49
  • and technically speaking, you're missing a closing brace for `if(filter_var($_SESSION['email'], FILTER_VALIDATE_EMAIL)) {` in your question. So, how about some feedback now? – Funk Forty Niner Jun 03 '15 at 17:53
  • @Fred-ii not `query`, but the connection. `prepare` was called for the connection before query was there :) – Hanky Panky Jun 03 '15 at 17:56
  • @Hanky웃Panky you're right; it's definitely their connection. – Funk Forty Niner Jun 03 '15 at 18:11
  • Any idea what is wrong with it? It worked before so there must be some minor issue I am missing... – Mark Jun 03 '15 at 18:17
  • Use some basic error checking from PDO Manual that will tell you the exact error – Hanky Panky Jun 03 '15 at 18:18
  • might be because of the constants. try the standard method as per the manual http://php.net/manual/en/ref.pdo-mysql.connection.php – Funk Forty Niner Jun 03 '15 at 18:22
  • @Mark I figured out the problem. Please reload the answer I have provided below, since it has been edited in order to reflect the reason why it is failing. – Funk Forty Niner Jun 04 '15 at 15:41
  • @Hanky웃Panky I figured it out; missing password parameter. *quick update* ;-) I knew it had to be something "missing". – Funk Forty Niner Jun 04 '15 at 15:57
  • Fred the second rocks! – Hanky Panky Jun 04 '15 at 16:25
  • @Hanky웃Panky hehe thanks Hank ;-) – Funk Forty Niner Jun 04 '15 at 16:34

1 Answers1

4

Edit: (I figured it out).

I finally figured out why your code is not working and my original answer no longer applies, which I have stricken out.

The reason why your connection does not work, is because you left out the dbpass constant from the connection parameter.

$db = new PDO(
"mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,dbpass,
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
) );

Original answer, which no longer applies. (see edit above).

TBH, I wasn't able to reproduce the error, amidst a great effort.

However; after tinkering with this, have discovered that PDO (least, the one on my server), won't allow for constants be used as the first 2 parameters in the DSN.

Sidenote: If what you say worked at your school, then there may be a setting used that I don't know about.

You can however, assign variables to the constants, then use those variables in the DSN.

$servername = "localhost";
$dbname = "user";

define("dbuser", "user");
define("dbpass", "");

$dsn = "mysql:host=$servername;dbname=$dbname";

$username = dbuser; // variable equals constant
$password = dbpass; // variable equals constant

$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
); 

$db = new PDO($dsn, $username, $password, $options);

For more information on PDO connection, visit:


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You should use try/catch statement for PDOException, it will point you right away that the password is wrong, see this answer: http://stackoverflow.com/a/36287723/591257 – aesede Mar 29 '16 at 14:51