473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

using my database class inside other functions - "Fatal error: Call to a member"

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
Aug 15 '07 #1
4 2505
Well I took it upon myself to add some debugging code and wallah, i'm now getting somewhere... my SQL query was failing, and because I was too lazy to add in some error checking I got a really unhelpful error.

I recommend the following to everyone:

[PHP]$result = $db->query($query);
if($result === FALSE)
{
die("Query failed: $query <br>\n".$db->error);
} [/PHP]
Aug 15 '07 #2
pbmods
5,821 Expert 4TB
Heya, dougans. Welcome to TSDN!

Were you able to get it working?
Aug 15 '07 #3
thanks for the welome and yeah, i got it going, got a new problem though and going to post it in a couple of minutes.
Aug 16 '07 #4
pbmods
5,821 Expert 4TB
Heya, Dougans.

Sounds good. We'll be ready for you :)
Aug 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
3
by: Randy Yates | last post by:
Hi, Is there a way to write a class member function that does not require an instantiation of the object to be invoked? For example, class MYCLASS { bool CheckIfMYCLASS(string &teststring);...
15
by: Steve Jorgensen | last post by:
I'm looking for coding style opinions. It looks to me like most VB/VBA coders use the command-style syntax for calling subs or functions being treated as subs in a particular context ... Foo...
2
by: Terry Fleming | last post by:
I have upgrade a 97 access database to 2003. I have a vb6 front end that I am trying to connect to the database. I am getting the "3343 Unrecognized database" error. I checked the references and...
1
by: timbobd | last post by:
I have encountered a situation that I don't understand. When I call a sub of Friend scope (in an object with Friend scope), I am getting an error "Public member 'subname' not found in type...
5
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem...
3
by: s.z.s | last post by:
Hi! I hope the solution to that is not too stupid... I've got three files: <snip test_main.cc> #include"test.hh" int main(void) { A<inta1; a1.saywhat();
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
3
by: sachinv1821 | last post by:
hi , i am Getting this Error fatal error C1189: #error : "eh.h is only for C++!" my Problem is i am Having C++ librabry and Appropriate .h file i want to Access Them is .c File Files..... to Be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.