Jump to content

can this go in an INCLUDE


Recommended Posts

I am creating several files with the same connection information.  I already REQUIRE_ONCE my connection data that has all the 'secret entry data', but am wondering, if this is my current code in the top of the files:



$con = require_once('connection.php');

if (!$con)


  die('Could not connect!' . mysql_error());



mysql_select_db("$database") or die('Choose a database  ' . mysql_error());


And then a $query/$result


Can the lines BEFORE the $query go into an INCLUDE file ALSO if they are going to be constant and repeated, or do they have to be hardcoded into each file individually?



Link to comment
Share on other sites

Are you talking about mysql_select_db()?  That can go into your connection.php.  So can the check for the connection.  Your common code in each file can look like this only:




As long as connection.php dies if anything goes wrong, there is no need to check the return value from require_once().

Link to comment
Share on other sites

require_once() is almost universally better.  The only situation you would consider using something else is if you wanted to include a file multiple times (which I have never had to in 7 years of professional PHP programming), or if you want your script to continue even if the file couldn't be included (again, I have never wanted this).  In those cases you would use require() or include_once() respectively.  include() is for if you want to include something multiple times AND you want to continue your script even if it fails.


For connection.php, require_once() is the right option.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • 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.