billckr Posted September 20, 2010 Share Posted September 20, 2010 Greetings all, I like to make a config.php file and place it outside my document root for security. I do this all the time. Then just inlcude the config in any file that I need information for. include('../includes/config.php'); This works most of the time for any DB connection information a piece of code might need. However, sometimes I like to place all my functions in a file called functions.php to keep them separate and have a single place to work with them. I have not done any coding in about two years and just started back and for the life of me I can't get any connection attempt inside any of my functions to use the DB connection info from the config.php even if I include the file right inside the functions.php at the top. Even when I put all my functions inside the config file itself they still have no access to the connection vars. I remember that I used to be able to use a global so some type to do this. The important part is getting DB connection access to any functions that I write. Here is an example of a plain config that I might start with; //* Database connection information.. *// $DBHost="localhost"; //* Database Hostname $DBName="some_db"; //* Database Name $DBUser="some_user"; //* Database Username $DBPass="some_pass"; //* Database Password $db = mysql_connect($DBHost, $DBUser, $DBPass); mysql_select_db($DBName); Can someone show me where I'm going wrong and the modern excepted method of doing this? I would greatly appreciated it!! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/ Share on other sites More sharing options...
Username: Posted September 20, 2010 Share Posted September 20, 2010 $host = "host"; $username = "username"; $password = "password"; $db_name = "db_name"; mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); You forgot your quotes or apostrophes around your vars in mysql_connect Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113249 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 No he didn't. Quotes around variables are unnecessary and actually makes the code execute a tiny bit slower. Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113251 Share on other sites More sharing options...
Username: Posted September 20, 2010 Share Posted September 20, 2010 No he didn't. Quotes around variables are unnecessary and actually makes the code execute a tiny bit slower. Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113252 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 If you only use a single database connection in your script, it is already available inside functions because the mysql_ statements use the last created connection by default. However, if you need more than one connection or you are writing your code to be general purpose so that it uses a specific connection, the preferred method would be to pass the connection into the function as a parameter when the function is called. Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113254 Share on other sites More sharing options...
billckr Posted September 20, 2010 Author Share Posted September 20, 2010 If you only use a single database connection in your script, it is already available inside functions because the mysql_ statements use the last created connection by default. However, if you need more than one connection or you are writing your code to be general purpose so that it uses a specific connection, the preferred method would be to pass the connection into the function as a parameter when the function is called . PFMaBiSmAd, Thanks for the reply. I'm glad to know that's how it's supposed to work but currently it's not 'could be my fault'. Using the config above if I have a funtion that like the below I get an mysql connection error that apache@localhost cannot connect meaning that it's not picking up my connection info for the config.php even if the function is written inside the actual config.php itself. <?php /* Select Clients from DB */ function client_select() { $con = mysql_connect("$BDhost", "$DBUser", "$DBPass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($DBName, $con); $result = mysql_query("SELECT * FROM some table"); while ($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } mysql_close($con); } ?> Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113263 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 A) You switched from talking about a connection to talking about the connection info. Those are not the same thing. B) You should not be making a connection per query (the function code you just posted.) A function who's purpose is to execute a query and return (or echo) the results of a specific query should not be making a database connection. For the code you posted, you would need to pass the $BDhost, $DBUser, and $DBPass connection info into the function as parameters when you call the function. Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113270 Share on other sites More sharing options...
billckr Posted September 20, 2010 Author Share Posted September 20, 2010 A) You switched from talking about a connection to talking about the connection info. Those are not the same thing. B) You should not be making a connection per query (the function code you just posted.) A function who's purpose is to execute a query and return (or echo) the results of a specific query should not be making a database connection. For the code you posted, you would need to pass the $BDhost, $DBUser, and $DBPass connection info into the function as parameters when you call the function. I don't think I switched what I was talking about. If I was misleading I apologize. or the code you posted, you would need to pass the $BDhost, $DBUser, and $DBPass connection info into the function as parameters when you call the function. Ok I think that is what I'm asking. How do I do this? Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113272 Share on other sites More sharing options...
billckr Posted September 20, 2010 Author Share Posted September 20, 2010 Ok, now that I know how to ask the question, I can find the answer. I think I need to be doing something like this. http://kimbriggs.com/computers/computer-software/php-mysql-connection-function.file Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113277 Share on other sites More sharing options...
billckr Posted September 22, 2010 Author Share Posted September 22, 2010 OK, I'm still not sure what I'm doing wrong here. I've tried every way I could think of. I really appreciate everyone's patience. Would someone be so kind as to "using the example below" show me how to pass the connection info from my config.php into the function below so I can use to select data from a database. config.php <?php $DBHost="localhost"; //* Database Hostname $DBName="dbname"; //* Database Name $DBUser="dbuser"; //* Database Username $DBPass="dbpass"; //* Database Password $db = mysql_connect($DBHost, $DBUser, $DBPass); mysql_select_db($DBName); ?> Fuction: <?php /* Select Clients from DB */ function client_select() { $result = mysql_query("SELECT FirstName FROM some table"); while ($row = mysql_fetch_array($result)) { echo $row['FirstName']; echo "<br />"; } mysql_close($con); } ?> So, the question is: How can I modify the function above or otherwise get the connection information from the config to it so I can place this fucntion in any place or any page that has my config.php included? Thank you for the assistance. Quote Link to comment https://forums.phpfreaks.com/topic/213894-site-wide-db-connection/#findComment-1113918 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.