svcghost — 2011-08-26T02:44:57-04:00 — #1
I grab a lot of stuff from my MySQL database on most pages of my site. Is there a better way to go about grabbing this data and making connections to the MySQL database than to include the connection string / code and disconnection strings each time I pull data?
$con = mysql_connect("localhost","toop","poop");
die('Could not connect: ' . mysql_error());
$result = mysql_query('SELECT *...
Or is it better to have a global .php file that always keeps the connection to the database open?
spacephoenix — 2011-08-26T02:54:20-04:00 — #2
If you use OOP then you could have a database class which gets passed around as needed. Are you closing the connections after each query or are you only closing the connection at the end of the script?
svcghost — 2011-08-26T03:14:37-04:00 — #3
usually just at the end of the script, or else it seems pointless to keep closing the connection
svcghost — 2011-08-28T16:48:31-04:00 — #4
anybody else have any input?
michael_morris1 — 2011-08-28T17:56:15-04:00 — #5
Unless you use persistent connections PHP itself closes all open connections when the script terminates even when (especially when) the script crashes. So it isn't something you need to worry too much about.
Using persistent connections isn't advised.
spacephoenix — 2011-08-28T17:59:20-04:00 — #6
Just open a single connection at the start of the script, normally php will automatically close any open connections when the script has finished running. If your not using OOP then you should create an include file in which you keep the database connection lines that have other scripts that need the db access include the file, unless your opening a another link php will make use of the already open link.
btw, you may want to consider migrating from the mysql_* functions over to the mysqli_* functions as I believe the mysql_* functions are being deprecated and so may well be removed at some point.
svcghost — 2011-08-28T18:20:10-04:00 — #7
svcghost — 2011-08-28T21:43:31-04:00 — #8
Would you recommend moving to MySQLi? My current site uses MySQL_ functions, not the "improved" class. Also, would you suggest using object oriented or procedural programming with MySQL_ class
michael_morris1 — 2011-08-29T11:17:24-04:00 — #9
I would suggest moving to PDO actually.
svcghost — 2011-08-29T11:49:25-04:00 — #10
May I ask why you recommend doing so?
michael_morris1 — 2011-08-29T14:51:29-04:00 — #11
Well for one, switching databases with PDO is simply a matter of changing the driver setting. For another, PDO features parameter binding which helps prevent SQL injection. For yet another reason, there has been talk of moving the other, older db libraries out of PHP core and leaving PDO in their place so they won't be loaded by default.
ahundiak — 2011-08-29T21:15:50-04:00 — #12
And furthermore, PDOs exception handling allows you to drastically the amount of error checking you need to do while at the same time helping to ensure that any errors that are encountered are handled. Much cleaner code.
svcghost — 2011-08-30T20:14:05-04:00 — #13