Page 1 of 1

PHP Class Question

Posted: Mon Mar 12, 2007 5:52 pm
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:

Posted: Mon Mar 12, 2007 6:13 pm
by RobertGonzalez
that code is not calling a connection for every query.

Posted: Mon Mar 12, 2007 6:20 pm
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?

Posted: Mon Mar 12, 2007 6:22 pm
by RobertGonzalez
What is wrong with the way it is now? Why the desire to use $this as opposed $db?

Posted: Mon Mar 12, 2007 6:39 pm
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..

Posted: Mon Mar 12, 2007 6:49 pm
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.

Posted: Mon Mar 12, 2007 7:13 pm
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?

Posted: Mon Mar 12, 2007 11:36 pm
by RobertGonzalez
If you are including the mysql.php file on each page, just put the instantiation of the object in that file.