Re: Efficient db connect function?
| From: | Bobby Patel | Date: | Sun, 23 Feb 2003 18:25:03 +0000 |
| Subject: | Re: Efficient db connect function? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-136745@lists.php.net to get a copy of this message | ||
As the previous post of using die would be better. Also try using
mysql_pconnect (for persistant connections) and also, if you are running
more than one query per script execution, then DON'T keeping trying to
connect/select database.
I suggest you do the first two things at the start of ALL the scripts that
will need to connect to the database, so make an include file with 2 things
(connecting and selecting a database). Then make another function just for
queries, this way on the second and onward queries will save overhead some
from connecting and selecting a database.
bobby
"Cf High" <noah@newminddesign.com> wrote in message
news:20030223175740.61268.qmail@pb1.pair.com...
> Hey all.
>
> I'm currently using the following db connection function, but I'm not sure
> if it's as efficient as it could be.
>
> function dbConnect($SQL) {
>
> global $result;
>
> // Connect to DB
> if (!$link = @mysql_connect($db_host, $db_user, $db_pass)) {
> $result = 0;
> echo mysql_errno() . ": " . mysql_error() . "\n";
> }
> else
>
> // Select DB
> if (!@mysql_select_db($db_name, $link)) {
> $result = 0;
> echo mysql_errno() . ": " . mysql_error() . "\n";
> }
> else {
> // Execute query
> if (!$result = @mysql_query($SQL, $link)) {
> $result = 0;
> echo mysql_errno() . ": " . mysql_error() . "\n";
> }
> }
> }
> return $result;
> }
>
> ******* Is there a faster way to establish a connection & run a query, or
am
> I wasting my time over nothing? *********
>
> --Noah
>
>