PHP Class Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

PHP Class Question

Post by aspekt9 »

I'm using a class I found for mysql functions and such. The way they implement it, you have to connect each time you want to use a function in the class. I want a way to make it so I won't have to call a connection each time I create an SQL statement. The example they use is:

Code: Select all

require_once('./mysql.php');
 $db = new MySQL;

 $db->connect(
        localhost,
        test_user,
        test_pass,
        test_db
 );

 $result = $db->query("SELECT * FROM test_table");
 echo $db->num_rows($result);
I want to put $db->connect(localhost,user,pass,db); Into a file such as config.php and use something like:

Code: Select all

$this->connect($host, $user, $pass, $db);
This way I only have to include one file such as config.php whenever I want to call an sql statement. How would I do this? If I try just using $this->connect in a config.php file I get an error:

Code: Select all

[B]Fatal error:[/B] Using $this when not in object context in I:\Apache\htdocs\test.php on line 5
I've tried just putting this into my config.php file but to no avail I receive another error:

Code: Select all

require_once('./mysql.php');
$db = new MySQL;

$db->connect(
        localhost,
        test_user,
        test_pass,
        test_db
);
I receive:

Code: Select all

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in I:\Apache\htdocs\class\mysql.class.php on line 91
Error: 0:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

that code is not calling a connection for every query.
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

so if I wanted to have a connection for everything in the class and put it into a file such as config.php and just include it all the time could I do that? Or would it be better to build it into the class? How would I do so?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What is wrong with the way it is now? Why the desire to use $this as opposed $db?
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

No, the problem is that I have to put:

Code: Select all

$db->connect(
        localhost,
        root,
        kywuhi,
        projects
);
In EVERY file that I want to call an sql statement..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Oh, I see what you are saying.

Are you including any files on those pages? Typically there would be a utility app that includes everything you need into the page you are on. Then those vars become available to you. If you are not including files now, then you are going to have create a file for inclusion, add that snippet to the new file, then include() that file where you want to use that functionality.
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Ok, that works, is there a way I can insert it directly into the class so I don't even have to include config.php?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you are including the mysql.php file on each page, just put the instantiation of the object in that file.
Post Reply