Pdo - Call to a Member Function Prepare() on a Non-object

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.

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.

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

Call to a member function prepare() on a non-object even after declaring global function

finally solved

function getUsers() {//returns an array of users
global $db;
$list = array();
$stmt = $db->prepare('SELECT username FROM user_info');
$users = $stmt->fetch(PDO::FETCH_ASSOC);
if (is_array($users) || is_object($users)) {
foreach ($users as $user) {
array_push($list, $user[0]);
}return $list;
}
}

$users = getUsers();

function getImageURL($user) {
global $db;
$stmt = $db->prepare('SELECT propic FROM user_info WHERE username=:username');
$stmt->execute(array(':username' => $user));
$source = $stmt->fetch(PDO::FETCH_ASSOC);
return $source;
}


Related Topics



Leave a reply



Submit