Page 1 of 1
require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 7:12 am
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.
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 8:20 am
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()
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 8:53 am
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"?
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 8:56 am
by Celauran
Code: Select all
if (!defined('FOO')) {
define('FOO', 'bar');
}
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 9:00 am
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?
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 9:06 am
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...
}
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 9:11 am
by simonmlewis
And leave the rest as is?
How is this better?
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 9:14 am
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?
Re: require, require_once, include: which and why?
Posted: Mon Mar 31, 2014 9:18 am
by simonmlewis
Gotcha.
Well I have updated the include to be include_once, and the database connection as you describe.