I am having a small issue with the following
public function getSiteName() {
$query = "SELECT name FROM siteinfo";
$result = $this->con->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)
", $row["name"]);
}
I do NOT get an error when connecting to the database however I get the following
Fatal error: Call to a member function fetch_array() on a non-object in /Users/russellharrower/Sites/evocca/etrading/system/core.php on line 15
I am wondering why would it not work? I used http://php.net/manual/en/mysqli-result.fetch-array.php
The engine am using is InnoDB
You can always try something like this:
Taken from: http://php.net/manual/en/mysqli-result.fetch-assoc.php
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)
", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
So After an hour of debugging I had to do the following.
For my project I am using a config.php as where I put all my Database connection defines.
config.php
<?php
define('DBServer','localhost'); // e.g 'localhost' or '192.168.1.100'
define('DBUser','root');
define('DBPass','PASSWORD');
define('DBName','ET5');
?>
core.php
<?php
class sys
{
protected $con;
public function __construct($con) {
$this->con = $con;
}
public function getSiteName() {
$query = "SELECT name FROM siteinfo";
$result = $this->con->query($query);
$res = array();
while ($row = $result->fetch_array()) {
$res[] = $row['name'];
}
return $res;
}
}
?>
Index.php
<?php
require_once("system/config.php");
require_once("system/core.php");
global $con ;
$con = new mysqli(DBServer, DBUser, DBPass, DBName);
if ($con->connect_errno)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sys = new sys($con);
$result = $sys->getSiteName();
print_r($result);
?>