Jump to content

can this go in an INCLUDE


phppup

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:

 

require_once('connection.php');

 

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