Hey there,
Hope someone can help me, completely stuck with immigrating from php4 to OOP based php5.
==
index.php
--
[PHP]include(database.inc.php);
include(functions.inc.php);
include(game.inc.php);[/PHP]
====
==
database.inc.php
--
[PHP]$query_count = 0;
class database extends mysqli
{
// Redefine the parent method
function query($query)
{
global $query_count,$query_log,$config;
$query_count++;
if ($config['query_logging'])
{
$query_log .= '<li>'.$query;
}
//echo "<br>counted query: total $query_count<br>\n";
return parent::query($query);
}
function return_first_result($query)
{
//
// EXAMPLE USAGE
//
//$test = $db->return_first_result("SELECT * FROM user_sleep");
//echo $test['assoc_id'];
return $this->query($query)->fetch_assoc(); // THIS IS LINE 45!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
$db = new database("localhost", "db", "pass", "db");
/* check connection */
# Check the connection and print an error and stop the page if it can't connect
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}[/PHP]
====
==
functions.inc.php
--
[PHP]function government_setting($setting)
{
global $db;
//$test = $db->return_first_result("SELECT * FROM user_sleep");
//echo $test['assoc_id'];
$gov_info = $db->return_first_result("SELECT $setting FROM `government_settings`");
return $gov_info[$setting];
}[/PHP]
====
==
game.inc.php
--
[PHP]echo government_setting('treasury_name');[/PHP]
====
Fatal error: Call to a member function fetch_assoc() on a non-object in /var/www/vhosts/europa100ad.com/includes/database.inc.php on line 45
I know the problem (or at least I think I do). The problem is with using the $db class inside my functions. I've tried adding global $db, or using the $GLOBALS['db'] and nothing seems to work...
My question is how can I use my $db class inside my functions?? I'm hoping I don't have to declare a new DB class in every function I plan to use the databse connection.
And I would prefer not to make my functions an extension of the DB class because I have currently 376 functions that are all split up and seperated evenly and neatly across the whole site. Just doing the tedious task of re-writting the site in PHP5, and I know most of my code would work in php5 but I want to give this Object Orientated Programming a bash for later use in other projects.
Thanks in advance, hope someone can help!
KD