Jump to content

Recommended Posts

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.

 

 

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!

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!

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.