473,769 Members | 7,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
Hey there,

Hope someone can help me, completely stuck with immigrating from php4 to OOP based php5.



==
index.php
--
[PHP]include(databas e.inc.php);
include(functio ns.inc.php);
include(game.in c.php);[/PHP]
====

==
database.inc.ph p
--
[PHP]$query_count = 0;

class database extends mysqli
{
// Redefine the parent method
function query($query)
{
global $query_count,$q uery_log,$confi g;
$query_count++;
if ($config['query_logging'])
{
$query_log .= '<li>'.$query;
}
//echo "<br>counte d query: total $query_count<br >\n";
return parent::query($ query);
}

function return_first_re sult($query)
{

//
// EXAMPLE USAGE
//
//$test = $db->return_first_r esult("SELECT * FROM user_sleep");
//echo $test['assoc_id'];

return $this->query($query )->fetch_assoc( ); // THIS IS LINE 45!!!!!!!!!!!!! !!!!!!!!!!!!!!
}
}

$db = new database("local host", "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.p hp
--
[PHP]function government_sett ing($setting)
{
global $db;
//$test = $db->return_first_r esult("SELECT * FROM user_sleep");
//echo $test['assoc_id'];
$gov_info = $db->return_first_r esult("SELECT $setting FROM `government_set tings`");
return $gov_info[$setting];
}[/PHP]
====


==
game.inc.php
--
[PHP]echo government_sett ing('treasury_n ame');[/PHP]
====


Fatal error: Call to a member function fetch_assoc() on a non-object in /var/www/vhosts/europa100ad.com/includes/database.inc.ph p 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 2535
dougans
6 New Member
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 Recognized Expert Expert
Heya, dougans. Welcome to TSDN!

Were you able to get it working?
Aug 15 '07 #3
dougans
6 New Member
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 Recognized Expert Expert
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
2496
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
2089
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
2126
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 "ABC", varDest Occasionally, I see code using the Call syntax instead. Call Foo("ABC", varDest)
2
5529
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 made sure that they are all matching. Does anyone have any advise on ow i can fix this -- Thanks
1
2235
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 'myType'". Any idea why? Since Friend scope should be visible within my assembly, and everying is taking place within a single project, I don't understand why this error is occuring. Here is some illustrative code, where I would get the error in the...
5
7246
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 is. Last time the problem was that "using namespace std" apparently had a "link" class/object defined already. Now I get: error: no matching function for call to 'String::String(String)'
3
2054
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
7112
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 someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
3
1828
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 More Specific i am Creating a C++ Object in a C file... this Giving me the error!!! :( can Any Body Please Help me
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.