The error I'm getting is: Here is the class casuing the problem. It's pretty simple, I have bolded the line 39 which is causing the problem: <?php /* Class: DbConnector Purpose: Connect to a database, MySQLi version */ require_once 'SystemComponent.php'; class DbConnector extends SystemComponent { private $mysqli; /* Function: __construct Purpose: Connect to the database */ function __construct() { // Load settings from parent class $settings = parent::getSettings(); // Get the main settings from array we just loaded $host = $settings['host']; $user = $settings['user']; $pass = $settings['pass']; $db = $settings['name']; // Connect to the database $this->mysqli = mysqli_init(); $this->mysqli->real_connect($host, $user, $pass, $db); } /* Function: query Purpose: Execute a database query */ function query($query) { return $this->mysqli->query($query); } /* Function: fetchArray Purpose: Get array of query results */ function fetchArray($result) { return $result->fetch_array(MYSQLI_ASSOC); } /* Function: close Purpose: Close the connection */ function close() { $mysqli->close(); } } ?> Code (markup): And here is the code: <?php require_once 'includes/DbConnector.php'; $connector = new DbConnector(); [B]$result = $connector->query("SELECT article FROM table");[/B] $connetor->fetchArray($result); ?> Code (markup): Anybody can help me solve this? Basically, I don't know why but $mysqli object is not available in query() function even though it is declared in class constructor.
Try function query($query) { global $mysqli; return $mysqli->query($query); } PHP: Regards, Steve EDIT: I believe this should also work, creating a class variable and accessing it through the class ($this) <?php /* Class: DbConnector Purpose: Connect to a database, MySQLi version */ require_once 'SystemComponent.php'; class DbConnector extends SystemComponent { var $myMySQLi = null; /* Function: __construct Purpose: Connect to the database */ function __construct() { // Load settings from parent class $settings = parent::getSettings(); // Get the main settings from array we just loaded $host = $settings['host']; $user = $settings['user']; $pass = $settings['pass']; $db = $settings['name']; // Connect to the database $this->$myMySQLi = mysqli_init(); $this->$myMySQLi->real_connect($host, $user, $pass, $db); } /* Function: query Purpose: Execute a database query */ function query($query) { return $this->$myMySQLi->query($query); } /* Function: fetchArray Purpose: Get array of query results */ function fetchArray($result) { return $this->$myMySQLi->result->fetch_array(); } /* Function: close Purpose: Close the connection */ function close() { $this->$myMySQLi->close(); } } ?> PHP: Untested, as I don't have access to PHP here, any problems let me know.
Nope that does nothing. I think the problem is the __construct() function hasn't been initiated. Don't know why because it's a constructor function so it should be processed every time new instance of the class is created.
Try the edited class above, try some echo's in the constructor before and after the mysqli code, then you know if it's being ran or not. Regards, Steve
I have updated the class in the first post... query() function works now but fetchArray() doesn't. Error: I have bolded the problem line.
The problem is fetch_array() can only be used like this (which doesn't work): $result->fetch_array(); Code (markup): According to PHP documentation - http://sk.php.net/manual/en/mysqli-result.fetch-array.php
Ahh, i thought it was the same as MySQL (silly me) Does this work? function fetchArray($result) { return $result->fetch_array(MYSQLI_ASSOC); } PHP: If not, then i'm not sure - Not used MySQLi before. Regards, Steve
That's what I have there right now and I'm getting this error: Anyways, thanks for help Rep added I will play with it a bit, maybe I will find some solution.
Anybody got any idea? I've been playing with the code and trying different approaches but still it isn't working It's easy with MySQL but with MySQLi it's problematic cause everything is object oriented...
Ok. I have used var_dump($result); and found out that $result is bool(false) - so even DbConnector::query() metho doesn't work, even though there is no error... Anybody could copy the script and try it on you localhost?
Problem solved The table was called 'table' which is a so called MySQL reserved word. I have renamed the table and everything works