phppup Posted March 12, 2012 Share Posted March 12, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258779-can-this-go-in-an-include/ Share on other sites More sharing options...
btherl Posted March 12, 2012 Share Posted March 12, 2012 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(). Quote Link to comment https://forums.phpfreaks.com/topic/258779-can-this-go-in-an-include/#findComment-1326609 Share on other sites More sharing options...
phppup Posted March 12, 2012 Author Share Posted March 12, 2012 Is REQUIRE ONCE the best method versus REQUIRE or INCLUDE? I've read lots of links, but am curious to hear from a REAL user. Quote Link to comment https://forums.phpfreaks.com/topic/258779-can-this-go-in-an-include/#findComment-1326615 Share on other sites More sharing options...
btherl Posted March 12, 2012 Share Posted March 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258779-can-this-go-in-an-include/#findComment-1326617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.