-1

I have database.php that already has require_once('../config.php') inside it. I don't understand why I still need to put both require_once('../config.php') and require_once('../database.php') in my index.php instead of require_once('../database.php') only since require_once('../config.php') is already inside database.php?

If I remove require_once('../config.php') in the index.php I am getting error.

<pre>Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in C:\xampp\htdocs\lyndaphoto\includes\database.php on line 18

Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\xampp\htdocs\lyndaphoto\includes\database.php on line 18

Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\xampp\htdocs\lyndaphoto\includes\database.php on line 18

Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\lyndaphoto\includes\database.php on line 18

Warning: mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (trying to connect via tcp://DB_SERVER:3306) in C:\xampp\htdocs\lyndaphoto\includes\database.php on line 18

Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\lyndaphoto\includes\database.php on line 18</pre>

config.php
$server = "localhost";
$user = "root";
$db_pass = "password";
$db_name = "photo_gallery";

define("DB_SERVER", $server);
define("DB_USER", $user);
define("DB_PASS", $db_pass);
define("DB_NAME", $db_name);


database.php

require_once("config.php");

class MySQLDatabase {


private $connection;    

function __construct() {
$this->open_connection();
}

public function open_connection() {
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
}else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}

public function close_connection() {
if(isset($this->connection)) {
mysql_close($this->connection);
unset($this->connection);
}
}
}

$database = new MySQLDatabase();
$db =& $database;

both files are on "localhost/photogallery/includes/" Thanks in advance! :)

Phil
  • 157,677
  • 23
  • 242
  • 245
arjay0601
  • 475
  • 2
  • 8
  • 16
  • You must have another `config.php` file in the same directory as your `index.php` – Phil Oct 01 '12 at 04:35
  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – tereško Oct 01 '12 at 12:01

2 Answers2

2

All includes in your code are based on index.php (or running script), so require_once("config.php") will search the file on same dir of index.php. Try the following on database.php:

require_once(dirname(__FILE__) . "/config.php");
Tasso Evangelista
  • 1,612
  • 1
  • 16
  • 23
  • You can also use the `__DIR__` magic constant in PHP 5.3+. Also, it's best to use `require` / `include` without parenthesis, eg `require_once __DIR__ . '/config.php';` – Phil Oct 01 '12 at 04:32
  • @Phil Great add to my comment. – Tasso Evangelista Oct 01 '12 at 04:33
  • @Phil why is it best to use without parentheses? Seems like it'd just be a style issue... – siride Oct 01 '12 at 04:36
  • 1
    @siride Because include/require are language constructs, not functions. – Tasso Evangelista Oct 01 '12 at 04:37
  • @siride See http://www.php.net/manual/en/function.include.php#example-139 (and the paragraph just above) – Phil Oct 01 '12 at 04:37
  • They aren't required, but just as people use parentheses after return, it doesn't seem unreasonable to use them after include/etc. A style issue, really, especially if the expression is some sort of compound like in this answer. To me, it looks better. – siride Oct 01 '12 at 04:40
  • @siride did you see the example I linked to above? The results of the expression are entirely different – Phil Oct 01 '12 at 04:41
  • @Phil: I guess that makes sense if you are using include inside another statement, but I don't see why it matters if include sits by itself. – siride Oct 01 '12 at 04:47
  • @Phil: and, you can still use parenthesis in the if-statement example. You can say `if((include('foo.php')) == OK)`. You just have to remember the extra outside parentheses. As far as I'm concerned, the redundant ones are irrelevant to language correctness. – siride Oct 01 '12 at 04:48
  • @siride This need to add extra parentheses brings me to use includes without parenthesis, avoiding errors. :) – Tasso Evangelista Oct 01 '12 at 04:51
  • @tassoevan: the redundant parentheses don't cause the errors. Lack of outer parentheses causes the error. – siride Oct 01 '12 at 04:51
  • @siride But the outside parentheses don't solve this on expressions using `include`? – Tasso Evangelista Oct 01 '12 at 04:57
  • @tassoevan: yes they do. That was the point of the example in the link. The outside parentheses limit the scope of `include` so that it doesn't include the ` == 'OK'` bit, which would make the argument `('vars.php') == 'OK'`, which does not evaluate to a filename. – siride Oct 01 '12 at 14:10
0

An alternative way to handle your include files is to make a single file called includes.php. It would contain all of your other resources.

// Filename: includes.php

require_once 'database.php';
require_once 'config.php';

Then, in your index.php and other documents you can access all of your application's resources by including that includes file.

require_once 'includes.php';
Sajan Parikh
  • 4,668
  • 3
  • 25
  • 28