Fatal Error: Call to a Member Function Prepare() on a Non-Object In

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 (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");

php fatal call to member function prepare() on non object

I think your problem is here:

try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
}

Add this line to catch after the line you have there:

var_dump($this->error);exit();

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.



Related Topics



Leave a reply



Submit