require, require_once, include: which and why?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

require, require_once, include: which and why?

Post by simonmlewis »

Code: Select all

[Mon Mar 31 11:07:25 2014] [error] [client 180.76.5.27] PHP Notice:  Constant DBHOST already defined in /var/www/vhosts/site.co.uk/httpdocs/dbconn.php on line 9
[Mon Mar 31 11:07:25 2014] [error] [client 180.76.5.27] PHP Notice:  Constant DBUSER already defined in /var/www/vhosts/site.co.uk/httpdocs/dbconn.php on line 10
[Mon Mar 31 11:07:25 2014] [error] [client 180.76.5.27] PHP Notice:  Constant DBPASS already defined in /var/www/vhosts/site.co.uk/httpdocs/dbconn.php on line 11
[Mon Mar 31 11:07:25 2014] [error] [client 180.76.5.27] PHP Notice:  Constant DBNAME already defined in /var/www/vhosts/site.co.uk/httpdocs/dbconn.php on line 12
I am getting these kinds of errors. I don't know why, since the site is running very fast and efficiently.
I'm also getting occasional ones like this:

Code: Select all

[Mon Mar 31 11:07:25 2014] [error] [client 180.76.5.27] PHP Warning:  mysql_query(): Access denied for user 'root'@'localhost' (using password: NO) in /var/www/vhosts/site.co.uk/httpdocs/index.php on line 838
We use PDO for connections. At the very top of our template is
include "databaseconn.php";

But should I be using require, or require_once? If it's "include" does that mean it constantly runs it, yet require is less hard on the DB? I'm not sure of the difference.

The closure of the connections happens right at the end, so the mysql_query issue is a real puzzle.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: require, require_once, include: which and why?

Post by Celauran »

I would definitely go include_once or require_once in this case. You can also wrap your defines in a conditional to ensure you aren't trying to define something that has already been defined. There are a couple of inconsistencies in what you've posted, though, that may also warrant investigation:
include "databaseconn.php";
...
PHP Notice: Constant DBHOST already defined in /var/www/vhosts/site.co.uk/httpdocs/dbconn.php
We use PDO for connections
...
PHP Warning: mysql_query()
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: require, require_once, include: which and why?

Post by simonmlewis »

Yes I copied that error, and wrote it in wrong. It is dbconn.php. My bad!
We use PDO, but there are "some "instances of mysql_query. Hence why our dbconn file has the option for both.

How do you "wrap your defines"?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: require, require_once, include: which and why?

Post by Celauran »

Code: Select all

if (!defined('FOO')) {
    define('FOO', 'bar');
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: require, require_once, include: which and why?

Post by simonmlewis »

Sorry, how do I define DBHOST, DBUSER etc, into a statement like that? I've never used DEFINEs before.

Code: Select all

if (!defined('DBSET'))
{
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'website');


....


$pdo = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPASS);
}
Like this?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: require, require_once, include: which and why?

Post by Celauran »

If you're defining them all in one block, either they're all defined or none are.

Code: Select all

if (!defined('DBUSER')) {
    define('DBUSER', 'username');
    define('DBPASS', 'password');
    etc...
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: require, require_once, include: which and why?

Post by simonmlewis »

And leave the rest as is?
How is this better?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: require, require_once, include: which and why?

Post by Celauran »

You're getting notices about constants already having been defined. We're ensuring they only ever get defined once. How is this not better?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: require, require_once, include: which and why?

Post by simonmlewis »

Gotcha.
Well I have updated the include to be include_once, and the database connection as you describe.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply