Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error Call to a member function prepare() on null [closed]

Tags:

php

mysql

pdo

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";}
like image 733
Mark Avatar asked Nov 20 '25 22:11

Mark


1 Answers

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"
) );
  • Even though you may not have a password set for it, it is still required to be part of the parameters.

  • http://php.net/manual/en/ref.pdo-mysql.connection.php


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:

  • http://php.net/manual/en/ref.pdo-mysql.connection.php


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.

like image 110
Funk Forty Niner Avatar answered Nov 22 '25 11:11

Funk Forty Niner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!