joshuasa9 Posted March 7, 2013 Share Posted March 7, 2013 Hello all, I am curious about something. I want to have a config.php file that consists of my connection string... pretty basic stuff. Now I have another file called functions.php which will hold the usual functions we use throughout the site. Now I just switched to mysqli for obvious reasons. And using this method, the mysqli_query function takes in 2 parameters, the connection link and the query you want to run. Now I have included the config.php file, but as of now I'm stumped of how to properly call that connection.. Would it be as simple as in the config file defining a variable to the connect function and calling that from the functions.php file? I tried that and got this result: Notice: Undefined variable: dbconn in functions.php.... Stumped. Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/ Share on other sites More sharing options...
joshuasa9 Posted March 7, 2013 Author Share Posted March 7, 2013 I just tried in the config.php creating a function that returns the connection and then in the functions.php file just put $link = GetConnection(); in every function in that file and it worked fine... Just curious if there's a way around this that is more... universal? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/#findComment-1417136 Share on other sites More sharing options...
Barand Posted March 7, 2013 Share Posted March 7, 2013 Does your GetConnection() function call mysqli_connect() every time you call it? So are you, therefore, reconnecting to the server every time you want to run a query? Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/#findComment-1417160 Share on other sites More sharing options...
joshuasa9 Posted March 7, 2013 Author Share Posted March 7, 2013 Does your GetConnection() function call mysqli_connect() every time you call it? So are you, therefore, reconnecting to the server every time you want to run a query? Yes... Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/#findComment-1417187 Share on other sites More sharing options...
trq Posted March 7, 2013 Share Posted March 7, 2013 It's pretty unclear as to what the problem is but me guess is that your not understanding variable scope. Variables declared outside of a function are not available inside of a function and vs versa. Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/#findComment-1417191 Share on other sites More sharing options...
joshuasa9 Posted March 7, 2013 Author Share Posted March 7, 2013 I guess what I'm getting at is this: using mysqli_... the methods for query, real_escape_string and many other methods require we put in the db link as an argument. Now does best practice now-a-days consist of always re-connecting to the database each time you want to run a query? Right now I have it setup so I include my config.php file which has a function GetConnection() that returns this: $link = mysqli_connect($db_host, $username, $password, $db_name) or die($connect_error); Then I have a functions.php file that has 3 functions that query the db. Each of those right now has this as the first link in the function: $dbconn = GetConnection(); and then I use that $dbconn variable to pass it in the mysqli_query method. I understand the variable scope and that variables declared outside functions are not available, I'm just curious as to the best practice when it comes to mysqli and connecting to the database. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/#findComment-1417199 Share on other sites More sharing options...
Barand Posted March 7, 2013 Share Posted March 7, 2013 The slowest part is connecting to the server - you do this once per page (unless you need to connect to more than one server) In your included file, create your $link variable then pass this as an extra parameter to each function that needs it. Connecting for every query will really hit performance. Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/#findComment-1417224 Share on other sites More sharing options...
DavidAM Posted March 7, 2013 Share Posted March 7, 2013 ... and if you never close the connection, your server will eventually run out of "slots" for connections and the function (and queries) will fail. Quote Link to comment https://forums.phpfreaks.com/topic/275352-mysqli-connection-and-access-throughout-site/#findComment-1417244 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.