I've search everywhere for a solution to this problem but nothing has sorted it. There a few questions similar to this on stack but I am confused as to whether I am implementing the solutions properly and it is another issue or if I'm wrong. I would like to know if any has any insight to the problem. I need to access a database from another class but I get the error Fatal error: Call to a member function prepare() on a non-object in demo.php on line 40. Here is my db_connect.php
<?php
class DbConnect {
function __construct() {
}
/**
* Establishing database connection
* @return database connection handler
*/
function connect() {
$db = null;
if (isset($_SERVER['SERVER_SOFTWARE']) &&
strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false) {
// Connect from App Engine.
try{
$db = new pdo('mysql:unix_socket=/cloudsql/projectid:instance;dbname=guestbook', 'root', 'password');
}catch(PDOException $ex) {
die('Unable to connect.');
}
}
$db = null;
}
}
?>
Here is where the error is coming from in demo.php
<?php
class Demo {
private $conn;
function __construct() {
require_once dirname(__FILE__) . '/include/db_connect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
public function getAllChatRooms() {
$stmt = $this->conn->prepare("SELECT * FROM chat_rooms");
$stmt->execute();
$tasks = $stmt->get_result();
$stmt->close();
return $tasks;
}
public function getAllUsers() {
$stmt = $this->conn->prepare("SELECT * FROM users");
$stmt->execute();
$tasks = $stmt->get_result();
$stmt->close();
return $tasks;
}
public function getDemoUser() {
$name = 'Joe Bloggs';
$email = 'admin@somewebsite.info';
*Error
40 ->* $stmt = $this->conn->prepare("SELECT user_id from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
if ($num_rows > 0) {
$stmt->bind_result($user_id);
$stmt->fetch();
return $user_id;
} else {
$stmt = $this->conn->prepare("INSERT INTO users(name, email) values(?, ?)");
$stmt->bind_param("ss", $name, $email);
$result = $stmt->execute();
$user_id = $stmt->insert_id;
$stmt->close();
return $user_id;
}
}
}
?>
In the function __construct am I not getting an instance of the database and assigning it to $conn ? I don't understand if this is correct or not. I have changed around DBConnect around to see if mysqli would work instead of pdo but that didn't fix anything. If anyone can shed some light on this I will be very grateful.
edit:
I'll rephrase what I am looking for. I would like to know if there is a way to connect mysqli to Google App Engine with SQL so that the pdo issue can be avoided. pdo is only used when connecting to the database and if I can change that to mysqli that I should be able to communicate with demo.php without having to change the methods such as bind_param that is not supported by pdo. Correct? If there is a simple way to update demo.php to work with pdo I am equally happy to do that but I don't know what methods are supported by pdo and I don't know php as a whole. What is the simplest option from a coding point of view and is it possible to use mysqli with GAE or does it have to be pdo? If GAE does not support mysqli can someone please tell me how to go about update the functions in demo.php?