...

View Full Version : mysql_connect



alexpayne
05-09-2006, 12:34 AM
how often should i call this function??

should i call it at the very beginning and use it throughout all the include()'ed pages?

or (as i did before) use mysql_connect for every single function?

lansing
05-09-2006, 01:43 AM
how often should i call this function??

should i call it at the very beginning and use it throughout all the include()'ed pages?

or (as i did before) use mysql_connect for every single function?I just make a file & require() the file whenever I have to communicate with the database. Example file below:


<?php
$user_name = "username"; //DB user name
$pass_name = "password"; //DB password
$db_name = "databasename"; //DB Name

/** I use this in all query strings so in case I need to rename a table
I don't have to change the table name in all my files. I just change
it here 1 time & not every page in my site **/
$table_one = "table_one_name"; //Orders Table
$table_two = "table_two_name"; //Customers Table
$table_three = "table_three_name"; //Referral System Table


//this connects to the database
$conn = mysql_pconnect("localhost", "$user_name", "$pass_name") or die(mysql_error());

//this selected the dabase. The DB is defined above with $db_name
$db_selected = mysql_select_db("$db_name", $conn) or die(mysql_error());

//this will display if the connection was made successfully
if (!$db_selected) {

//this is the text that displays if the DB connection wasn't made!
die('Could not connect to database: ' . mysql_error());
}

//this is the text that displays if the DB connection was made!
echo '';
?>

Beagle
05-09-2006, 04:31 AM
You should be connecting as little as possible. If you're only using one database, connect at most once per script. Dont' disconnect until the end of the script.

lansing
05-11-2006, 04:22 PM
You should be connecting as little as possible. If you're only using one database, connect at most once per script. Dont' disconnect until the end of the script.Is the once per script a security issue or a bandwidth/resources issue? I just call my connection script only when I have to connect...I just did this since I thought it would use less bandwidth/resources.

guelphdad
05-12-2006, 12:56 AM
it is a bandwidth/usage issue. normally the number of connections you have to your database is limited. if you have a site with numerous users opening 5 or 10 connections at a time you can find yourself running out of connections, and users not able to connect to your site.

it is also resource intensive if you have to continually reconnect to the database.

lavinpj1
05-12-2006, 01:07 AM
it is a bandwidth/usage issue

How can you use bandwidth when connecting to localhost?

~Phil~

Kid Charming
05-12-2006, 05:40 AM
If you have more than one in a single script, it won't use up all your db connections. Subsequent mysql_connect() calls will just return the existing connection instead of making a new one unless you specifically force it to.

Of course, this means all those extra calls are just eating resources for nothing, though, so you should still get rid of them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum