Development Tools Using PHP with MySQL Querying the Database with PHP Avoiding Duplication

Leigh Maher
Leigh Maher
20,340 Points

How does the database.php know to get the values we defined in the config file?

I notice that Randy didn't include the config file in the database.php, but it still works fine.

How does the database.php know to get the values we defined in the config file?

3 Answers

Kevin Korte
Kevin Korte
28,096 Points

Because when the code is actually executed, the config data is there with the database. On files that show product, he has two files required...config and products. In the product file, he requires the database file.

So when this file gets executed, the config data comes in, the product data comes in, and the database file comes in. When you call a require, its like copying and pasting the code from that file into the file that has the require. So "technically" all the information is there at run time, and it doesn't have to "look" for the information.

I don't think I was clear there, but does that help?

Leigh Maher
Leigh Maher
20,340 Points

Thanks for the reply, Kevin.

So, I can see that in products.php he requires the database file, and on the shirt.php page he requires the products.php and config.php files. So, are you saying that when shirt.php loads, it effectively facilitates the link between the the config.php and database.php file?

Kevin Korte
Kevin Korte
28,096 Points

Yes, exactly, so when it loads the shirt.php file, it goes in down the file, first loading config.php, than it loads products.php, and while it's doing that, it loads the database.php, so as the PHP server views it, all the code from config.php, product.php, and database.php are loaded and loaded in an order where nothing is called before it's used.

Leigh Maher
Leigh Maher
20,340 Points

Great. Thanks for the explanation, Kevin.