Call to a Member Function Prepare() on a Non-Object PHP Help

pdo - Call to a member function prepare() on a non-object

$pdo is undefined. You're not declaring it inside the function, and it isn't being passed in as an argument.

You need to either pass it in (good), or define it in the global namespace and make it available to your function by placing global $pdo at the top (bad).

Fatal error: Call to a member function prepare() on a non-object in

You are trying to call prepare() on a wrong object. It's not $conn, but $mysqli.

Fatal error : Call to a member function prepare() on a non-object in

PHP raised this error because, the member function called on the non-object. The issue because to initialization of object my_sqli is not done properly. Before calling prepare method, the object must be initialized and connection should parameters must be passed appropriately so, it creates successful connection and returns object.


$host = "my_host"; //"localhost" or "http://mysql.host.com"
$user = "my_username"; //an authorized user of the MySQL database
$password = "my_password"; //my_username's password
$database = "my_database"; //the database we want to use.
$mysqli = new mysqli($host, $user, $password, $database);
if ($mysqli->connect_error)
{
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$stmt = $mysqli->stmt_init();
if ($stmt->prepare("some valid SQL query"))
{
$stmt->bind_params("s...", $vars);
$stmt->execute();
$stmt->bind_result($vars);
while ($stmt->fetch()) {
//do stuff with the data
}
$stmt->close();
}
$mysqli->close();

PDO Fatal error: Call to a member function prepare() on a non-object

There are several problems with your code.

Two were explained in the other answer, which will make your code work (eventually it all was spoiled), but it's still wrong approach, which will connect to database as many times as many objects you have.

Change DatabaseConnection class this way

class DatabaseConnection{
public $pdo;
public function __construct(){
$user = 'root';
$pass = '';
$dsn = 'mysql:charset=utf8;dbname=test;host=localhost;charset=utf8';
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
);
$this->pdo = new PDO($dsn, 'root', '', $opt);
}
}

Change LoginRegister constructor this way

function __construct($db){
$this->db= $db;
}

And make register.php this way

require_once "functions.php";
$db = new DatabaseConnection();
$user = new LoginRegister($db->pdo);

and then in LoginRegister use $this->db instead of $pdo all the way.

The main idea to make $db connection an external service for the application class. Otherwise it will be all the same as despised global, but just in another form.

PHP Fatal error: Call to a member function prepare() on a non-object

global should be used inside the function scope, not outside/before it.

function myfunc(){ // Do NOT pass anything called $mysql to this function!
global $mysql;
do_stuff_with_mysql();
}

Of course, it has to be defined before declaring your function (in your db_connect.php file) :

$mysql = new PDO($dsn, $user, $pass);

See : http://fr2.php.net/manual/en/language.variables.scope.php. Alternativaly, you may want to consider object-oriented programming :

class DB {
private $pdo;
public function __construct() { $this->pdo = new PDO(...); }
public function myfunction() { /* use $this->pdo here */ }
}

Important note : mysql_* functions are now deprecated. http://www.php.net/manual/en/book.pdo.php

PDO Fatal error: Call to a member function prepare() on a non-object (using classes)

the mysql libraries seem to interpret localhost as meaning you want to connect via a unix socket. your error indicates your mysqld isn't setup to accept unix socket connections.

to connect via tcp/ip instead, change define("DB_HOST", "localhost"); to define("DB_HOST", "127.0.0.1");

Call to a member function prepare() on a non-object PHP PDO

Make your User class like below :

class User extends Database {

private $name;
private $email;
private $date;

function __construct($name, $email) {

$this->name = $name;
$this->email = $email;
parent::__construct();
}

public function insert() {

$STH = $this->DBH->prepare("INSERT INTO `phppdo` VALUES(NULL, :name, :email, UNIX_TIMESTAMP())");
$STH->execute(array(
':name' => $this->name,
':email' => $this->email,
));
}
}

Call to a member function prepare() on a non-object PHP Help

It's a scoping error. You're making $DBH a global variable. So when you enter the function, the global variable is not available. You have 5 real options.

1. Use the global keyword

function doSomething() {
global $DBH;
//...

This is not a good idea, since it makes maintenance and testing a PITA. Imagine trying to debug that function call. You now need to go find out where $DBH is defined to try to figure out what's going on...

2. Make $DBH a parameter to the function

function doSomething(MySQLi $DBH) {

It has the advantage of being explicit. But it's still not great since the calling code then needs to keep track of the global variable.

3. Create a function to "get" the $DBH object

function getDBH() {
static $DBH = null;
if (is_null($DBH)) {
$DBH = new mysqli(...);
}
return $DBH;
}

function doSomething() {
$DBH = getDBH();
}

This has the advantage of getting around the global variable problem completely. But it's also hard to have multiple connections or re-use any of the code for other connections.

4. Create a class to wrap database access

class Database {
public function __construct($host, $user, $pass) {
$this->DBH = new MySQli($host, $user, $pass);
}
public function doSOmething() {
$this->DBH->foo();
}
}

This encapsulates everything for you. All database access will go through a single class, so you don't need to worry about global variable access or anything else.

5. Use a pre-built class/framework

This is the best option, since you don't need to worry about doing it yourself.

Database Access Classes:

  • A quick google search to get you started
  • Doctrine ORM - A complete database access library with full ORM (Object Mapping)
  • ADODB - A database agnostic database access library
  • Pear MDB2 - Another database access library

Full Frameworks:

  • Zend Framework
  • Lithium Framework
  • Code Igniter
  • (really there are a lot more, I'm not going to bother listing any more since that's another question all together...)

Really, the choices are endless. Find something you like, and stick with it. It really will make your life easier...



Related Topics



Leave a reply



Submit