Jump to content
Larry Ullman's Book Forums

Just How Do Config.inc.php And Mysqli_Connect.php Work?


Recommended Posts

I have spent several months trying to teach myself web programming. I've managed to create a pretty cool dynamic website that works OK - so far. Then I found this great book. I am now trying to incorporate some of these important ideas into my code. My first attempt was to use the ideas behind the use of the config.inc.php and mysqli_connect.php files as explained in CH 18. This became a disaster as I tried various schemes to make it work which caused me to revert to the previously saved version of my code and start over. Before I do I'd like to be sure I have a clear understand of what I'm trying to do. 

 

The four 'connect' parameters are 'defined' as constants in the mysqli_connect.php file. In the config.inc.php file I defined the constant MYSQL as the path to this mysqli_connect.php file. 

 

As I read chapter 18 it says that I can now - require MYSQL; - in any script where I need to connect to the database. Questions:

 

1) When I run the app I get an error saying that I can not redefine a constant. The mysqli-connect.php file does attempt to re-define the four connect constants every time it is run in a script where it was 'included' and so the error is totally expected. What am I not getting here?

 

2) If I 'include' this file many times in scripts that echo html to the browser would not the connect constants then appear on my pages and be readable to anyone who wishes to "view source" in their browser?  

 

I'm sure I'm missing some important basic concept here. Some more info that might be relevant:

 

I created a 'config' folder in both my localhost xampp development environment and on my GoDaddy server account right under the root directory. In both accounts I keep the config.inc.php file in there.

 

In the local account I have placed the mysqli_connect.php file in the same config folder. In the GoDaddy account I placed it in a directory just above the root. This way in the config.inc.php the 'define MYSQL' path is different in each account and point to the respective locations for the mysqli_connect.php file.

 

All files except 'config.inc.php' in the app can be the same this way. To migrate working code to the online account as I make improvements I just move all folders under the root in the xampp account to the GoDaddy server under the 'public_html' folder - except for the config folder which I should never have to touch.

 

3) Is this the correct way to do this? 

  

Regarding questions 1 and 2, I realize I have some very basic misunderstanding but I can't figure out what it is. Thanks.

Link to comment
Share on other sites

Hi, Ray, and welcome to the forums.

 

To answer #1, we will need to see your actual code.

Could you please post the relevant parts of your code (minus the connection credentials), and when you do, could you please use the "<>" button in the WYSIWYG editor to better format your code?

Thanks.

 

As for #2, PHP is run on the server, and as such, PHP code is not output to the browser. Naturally, anything you echo will be output to the browser, but so long as you don't echo your constants, etc. (which you should never need to), that information would never be displayed in the browser, and no one would ever see it (save those that have access to the PHP files on your server).

 

For question #3, there are a variety of ways to handle config files, but my personal favorite is to define a file called prod.php that is simply the following:

<?php
  
  $prod = true;

That's what it would look like in my production environment. In the development environment, $prod would be set to false. After you create that file once and have it properly set to true or false for each of your environments, you never have to edit it again.

From there, I include prod.php at the top of my config file as follows:

include('prod.php'); // Note that prod.php is in the same folder as the config file.

Then, any time after that that I want to know whether I am in production or not, I just check the $prod variable.

I generally also have the following towards the top of my config file (but below where I include prod.php):

if ($prod) {
  // Set up production DB connection.
  // Set production environment constants.
} else {
  // Set up development DB connection.
  // Set development environment constants.
}
By doing things that way, you can easily copy all of your files over from the development environment to the production environment (including your config file) at will, and so long as you never touch the prod.php file, you're fine.
You don't have to edit any of your files, ever.
 
Hope that all helps.
  • Upvote 1
Link to comment
Share on other sites

Hi HartletSan, Your answer to #3 makes perfect sense. It's more elegant than the one I came up with. Thanks for that.

 

As for problem #1, I figured it out in the process of writing you a long explanation - which I erased. In CH18 Larry clearly says that the config.inc.php file is "included" on every page of the app. Somehow my brain read that as every "file" rather than every "page". 

 

My main page is a large table, about 18 columns by from a dozen to as many as a few hundred vertically scrolling rows. I build this main page using a primary php file that sets it up and then two other supporting php files which are "included" by the main file and access the database to fill in the cells".

 

I was including the MYSQL constant and the config.inc.php file in each of the three files I used to construct the page - when, as I see now, I only needed to include each of the two configuration files once, for the whole page.

 

I now have the config.inc.php file at the very start of the main file and the defined constant MYSQL included just before I first need the data base in the first supporting file.

 

It all runs fine now and I learned something important.

 

Thanks for being there. I'm glad I found this resource and also that I bought this great book. Every page is full of good stuff. I just have to read it more carefully.

 

Ray P

Link to comment
Share on other sites

Yes, writing require(MYSQL) will take the contents of the mysqli_connect.php file and dump them in that exact location of the file including the MySQL DB connection file.

Also, yes, having require(MYSQL) more than once will cause the error you are seeing with attempting to redefine already defined constants.

 

In order to help you beyond that though, I need to see the code for the script that is including the other two PHP files.

Please show us that code too, and we should be able to help.

Thanks.

Link to comment
Share on other sites

Sorry if my updated response above confuses anyone. But I solved my problem while attempting to explain it in that comment. So I edited the comment since it hadn't been answered at that time. (early this morning US Pacific coast time)  But, HarleySan answered the unedited version somehow. I think next time I'll just add another comment rather than edit an older one. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...