papaface Posted December 27, 2007 Share Posted December 27, 2007 Hello, Can someone tell me if this is a good, and efficient way of connecting to a mysql database? The page is connect.php and would be included on all php files that require a database. Is this a good way, or is there a better way of doing it? <?php class DatabaseConnect { protected $dbname = "papaface_qss"; protected $dbusername = "papaface_default"; protected $dbpassword = "***"; protected $hostname = "localhost"; public $connection; function __construct() { $this->connection = mysql_connect($this->hostname,$this->dbusername,$this->dbpassword) or die (mysql_error()); mysql_select_db($this->dbname,$this->connection) or die ("Could not select the database: " . mysql_error()); //echo "Connected to the database."; } } $_conn = new DatabaseConnect(); ?> Thanks, any input would be great Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/ Share on other sites More sharing options...
Gamic Posted December 28, 2007 Share Posted December 28, 2007 This is the way I connect. There is only one more feature your connection could have to make it better, a query($sql) method that returns either the entire results, true, or false (depending on the type of query, etc etc). I'm sure that others will have different ways of connecting. Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-424468 Share on other sites More sharing options...
trq Posted December 28, 2007 Share Posted December 28, 2007 If the only intention of said class is to connect to a database (and your not planning on extending it), then your really don't need a class at all. Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-424514 Share on other sites More sharing options...
Liquid Fire Posted December 28, 2007 Share Posted December 28, 2007 That is a start of a database class. That alone is not very useful but add functions for querying use active record pattern stuff, and then you have something very useful. Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-424743 Share on other sites More sharing options...
papaface Posted December 29, 2007 Author Share Posted December 29, 2007 If the only intention of said class is to connect to a database (and your not planning on extending it), then your really don't need a class at all. Yeah, I'm just making one incase I do intend on creating more function for it Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-425239 Share on other sites More sharing options...
Anthop Posted January 4, 2008 Share Posted January 4, 2008 I agree with thorpe. I would probably just do it functionally and return a connection pointer. If you intend to add more functions, you still don't need to make a class; you could simply just have a bunch of functions in connect.php file to help you manage and mess with your connection. Otherwise, there's really no problem with how you're doing it. In fact, if you define the __destruct() method to close your connection automagically, then this may actually be better! (ie: Add the following function to your class:) __destruct() { mysql_close($this->connection); } Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-430073 Share on other sites More sharing options...
Daniel0 Posted January 4, 2008 Share Posted January 4, 2008 In fact, if you define the __destruct() method to close your connection automagically, then this may actually be better! (ie: Add the following function to your class:) __destruct() { mysql_close($this->connection); } Database connection will be closed by PHP automatically at the end of script execution. There is no need to do it yourself. Also, you shouldn't close important things in destructors (file handles, database connections, sockets etc.) as you cannot control the order of which objects are destroyed. You should only close a database connection if you need it to end before the script ends. Otherwise there is no point in doing it. Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-430310 Share on other sites More sharing options...
Anthop Posted January 4, 2008 Share Posted January 4, 2008 @ Daniel: Didn't know that! There are some nice things about PHP ! My background is in desktop programming languages, especially strict, strongly typed ones like Java and C#, and in those cases, closing your connections is an iron rule of sorts. (Not as bad as deallocating memory in C, but still pretty strict.) So, I'm just very used to closing things when I'm done with it and making sure not to use it again. (I suppose I might get a very slight performance increase for it though?) In any case, PHP is very different in a lot of ways, so it's been throwing me for a loop . Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-430327 Share on other sites More sharing options...
Daniel0 Posted January 4, 2008 Share Posted January 4, 2008 I suppose I might get a very slight performance increase for [closing resources before the script ends] though? Not really. PHP detects when a resource is not used anymore frees it automatically. It's just a matter of who that does it: you or PHP's garbage collector. Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-430338 Share on other sites More sharing options...
svivian Posted February 4, 2008 Share Posted February 4, 2008 My own class that I wrote has a query() function that executes SQL and stores the result internally. I did this to add a bit of abstraction - I also have a row() function that returns the next row from the stored result, so I can do something like this in my code: while ( ($row=$db->row()) !== false ) { /* do the shiz here */ } Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-458080 Share on other sites More sharing options...
Stephen68 Posted February 14, 2008 Share Posted February 14, 2008 Hi! I'm really new to this but do have a question that is not to silly. In the first classes could he not set the values for the connection but make functions that assign the connection information? Stephen Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-466475 Share on other sites More sharing options...
trq Posted February 14, 2008 Share Posted February 14, 2008 Hi! I'm really new to this but do have a question that is not to silly. In the first classes could he not set the values for the connection but make functions that assign the connection information? Stephen Can you explain what you mean exactly? Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-466491 Share on other sites More sharing options...
aschk Posted February 14, 2008 Share Posted February 14, 2008 I think stephen means something like this: <?php class DatabaseConnect { protected $dbname; protected $dbusername; protected $dbpassword; protected $hostname; public $connection; function __construct($hostname,$username,$password,$database) { $this->hostname = $hostname; $this->dbusername = $username; $this->dbpassword = $password; $this->dbname = $database; $this->connection = mysql_connect($this->hostname,$this->dbusername,$this->dbpassword) or die (mysql_error()); mysql_select_db($this->dbname,$this->connection) or die ("Could not select the database: " . mysql_error()); //echo "Connected to the database."; } } $_conn = new DatabaseConnect("localhost", "papaface_default", "****", "papaface_qss"); ?> This way he could create more than 1 DatabaseConnect for different MySQL connections. e.g. <?php $_conn1 = new DatabaseConnect("localhost", "papaface_default", "****", "papaface_qss"); $_conn2 = new DatabaseConnect("123.456.789.12", "other_user", "some_password", "other_database"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-466719 Share on other sites More sharing options...
Stephen68 Posted February 15, 2008 Share Posted February 15, 2008 Yes that is what I was talking about, I'm sorry I was not that clear in my first post. I guess maybe I should not post after working a long long day and night. Cheers! Stephen Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-467280 Share on other sites More sharing options...
Stephen68 Posted February 15, 2008 Share Posted February 15, 2008 Is there a reason that you are using $_conn instead of say $conn? just wondering... Stephen Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-467312 Share on other sites More sharing options...
Daniel0 Posted February 15, 2008 Share Posted February 15, 2008 Prefixing a variable name with an underscore is a common convention meaning it's a private or protected property (or method if it's the method name). He does it outside a class though which is a bit unusual. Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-467453 Share on other sites More sharing options...
aschk Posted February 15, 2008 Share Posted February 15, 2008 Nope, just a copy and paste from your original post underscore won't make a difference at all (except in php4 i think it means protected member variable, can someone verify that?) Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-467542 Share on other sites More sharing options...
Daniel0 Posted February 15, 2008 Share Posted February 15, 2008 As I said, it's just a naming convention used by some people, nothing else. In PHP4 the following would work perfectly fine, but people might get confused: <?php class Test { var $_variable; // some methods } $obj = new Test(); $obj->_variable = 'Test'; echo $obj->_variable; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83374-is-this-a-good-way-of-connecting-to-a-database/#findComment-467557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.