Page 1 of 1
Best Way ???
Posted: Thu Nov 18, 2004 3:00 am
by thomas777neo
Hi All
I want to find out the best way of having one database connection class, I have a modified version of the example I am currently using:
class.mysql.php
Code: Select all
<?php
class mysql
{
protected function connection()
{
require("settings.php"); // Mysql Connection Settings
$connection = mysql_connect($host,$username,$password);
}
protected function selectDatabase()
{
require("settings.php"); // Mysql Connection Settings
$select = mysql_select_db($database);
}
}
$mysql = new mysql;
?>
class.testMysql.php
Code: Select all
<?php
class mysqlTest extends mysql
{
public function getData()
{
$connection = $this->connection();
$select = $this->selectDatabase();
$sql = "SELECT * FROM username";
$query = @mysql_query($sql);
$rows = @mysql_num_rows($query);
@mysql_free_result($query);
unset($connection);
return($rows);
} //public function getData()
} //class mysqlTest extends mysql
?>
Is this the most efficient way of going about the connection? Should I use the try...catch statement for error handling or the normal die statement?
Posted: Thu Nov 18, 2004 2:39 pm
by pickle
Seems pretty efficient. Some recommendations though:
1) In your mysql class, you only require settings.php once, then import the required variables into the object. Requiring files is relatively quite expensive in terms of cycles and resources, so the fewer require() and include() statements the better.
2) Is there a specific reason you've separated out your connection logic and your database selection logic? In my experience, I've never needed to select a database without first connecting, and I've never connected without selecting a database. I'd suggest putting the connection() and selectDatabase() logic in the same function. Doing so will a) reduce the number of function calls and b) eliminate the chance you'll call selectDatabase() without first calling connection()
3) You might as well put a function similar to getData in the mysql class as well. That way all your database interactivity is handled in one file (though you may be getting to that as I notice your second class is a "test" class).
4) I'm not really experienced with try...catch statements, but I do know they provide you with much more flexibility as to what to do when something fails. I'd recommend using try...catch because at least you can then recover from errors and failures.
Thanks
Posted: Mon Nov 22, 2004 4:28 am
by thomas777neo
Thanks Pickle
I have incorporated the changes you have suggested. I also scrapped the require("settings.php") idea. I wrote an encrypted username and password to the session, decrypted the variables and carried on from there. The problem with the require would be if you created a page two or three levels below the class level. The required page wouldn't be found.
Thanks Again
Posted: Mon Nov 22, 2004 9:43 am
by pickle
A solution around that would be to put the file in your include path. When PHP includes a file, it first checks the include path, then it checks the local directory. So, for example, if you had (/var/www/html/include) as your include path, and you called 'settings.php' from a file located in (/var/www/html/website/setup/). PHP would first look in (/var/www/html/include) for the 'settings.php' file and if it didn't find one, would then go on to (/var/www/html/website/setup/) to look for one.
Or, you could just declare the absolute path of the settings.php file, so it wouldn't matter where the calling page was.
Question
Posted: Tue Nov 23, 2004 6:06 am
by thomas777neo
The include path you mentioned, is it a configuration setting or do I specify it somewhere in my code?
Posted: Tue Nov 23, 2004 9:33 am
by pickle
It's a configuration setting. You can find it in php.ini.
Posted: Tue Nov 23, 2004 10:02 am
by djot
-
Hi,
Since pickle is to lazy

I did search for you:
http://www.php.net/manual/en/function.ini-set.php
http://www.php.net/manual/en/ini.sect.p ... clude-path
Code: Select all
ini_set('include_path','YOURINCLUDEPATHHERE');
djot
-
Posted: Tue Nov 23, 2004 10:06 am
by pickle
djot wrote:Since pickle is to lazy

I did search for you
Meh. (and "to" should be "too")

Posted: Tue Nov 23, 2004 2:29 pm
by fl0w
Hi, I'm new here
But I simply use:
Code: Select all
<?php
final class sql {
public static function construct() {
// .. connection
}
public static function query() {
// simply returns query results.
}
public static function destruct() {
// close the connection
}
}
?>
And then on the page I just do
sql::construct() , and voila, there's my needed connection.
I used to use a class that I passed to each function, but I've never needed multiple mysql connections, and passing the sql class to each function was ugly, also making it global is even more ugly.
The query function is only so I can ignore storing the query in a variable before using mysql_query.
Another Question
Posted: Thu Nov 25, 2004 4:45 am
by thomas777neo
Thanks for the feedback, it helps a great deal.
Flow-> your posted code
Code: Select all
final class sql {
public static function construct() {
// .. connection
}
public static function query() {
// simply returns query results.
}
public static function destruct() {
// close the connection
}
}
If I have another class, that needs to use this class, I won't be able to extend it (cannot inherit from class), because it's final (if I’m not mistaken). I am also new to everything, but what I am trying to achieve is have one connection class and them simple extend it, here is what I have done so far (this is an extremely watered down version):
Code: Select all
<?php
//class.mysql.php
class mysql
{
protected $connection;
public function __construct()
{
$this->connection = $connection;
}
protected function connection()
{
//connect
//select db
return($this->connection); // to get #resource id
}
}
?>
Code: Select all
<?php
//class.test.php
class test extends mysql
{
function testConnect()
{
$connection = $this->connection();
$sql = "SELECT count(*) FROM test";
//etc
}
}
?>
I'm just trying to see how to use it in the most efficient way, and how it would fit into my picture. Could you apply your example to my situation with some more detail in your example?
Posted: Thu Nov 25, 2004 12:07 pm
by jl
I'm not sure in every case you would want to extend your mysql class to use it. I programmed a large application by doing exactly that, and then wished that I'd done something like this instead, which I think is far more flexible:
(This is PHP4, I'm not very familiar with PHP5. In this exampe function foo() is the constructor)
Code: Select all
require_once('mysql.class.inc.php') ; // include your MySQL class definition here
class foo {
var $db ;
function foo() {
$this->db=new mysql() ;
$this->db->connection() ;
}
function dostuff() {
$this->db->query('SELECT...') ;
}
}
This also has the advantage that you can then extend foo to something more relevant than the db class, which isn't really subject specific, it's just a piece of functionality that you want to add in to your class - what if you want to add in 2 or 3 pieces of functionality...