HELP NEEDED on directory of share include file

//this is my index file on totaljokes-error( a folder name)

<?php
include_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;

function totaljokes()
{
try
{
$result=$pdo->query(‘SELECT COUNT(*) FROM joke’);
}
catch (PDOException $e)
{
$error=‘Database error counting jokes!’;
include ‘error.html.php’;
exit();
}

$row=$result-&gt;fetch();
return $row[0];

}

//Even my folder “includes” contained file “error.html.php” and “db.inc.php” in directory wamp/www, but the http://localhost/totaljokes-error/ showed nothing, just blank. Am i wrong placed my file or what else? I’m blur with the share include file in directory.

Any help is appreciated.

Try:
include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’);
Or use a relative path:
include_once ‘includes/db.inc.php’;

Thank you for willing to help. I had tried both just now, it still didn’t work, blank…no such file in directory when i used ‘includes/db.inc.php’; and still blank with the parentheses. ~.~…

What’s the value of $_SERVER[‘DOCUMENT_ROOT’] ? You mentioned you were running this on Windows…

You could try including “./includes/db.inc.php” (forcing a relative reference with .)

i had tried again, same, blank. I just want to know how to use include files in directory. By the way, i followed this php code from PHP & MySQL: Novice to Ninja book, page 177. Help…

The values appeared is C:/wamp/www/ for $_SERVER[‘DOCUMENT_ROOT’].
What I wrote was include_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;
I had try many times…the browser still go for blank.
includes folder contained db.inc.php, error.html.php
totaljokes-error folder contained index.php which i listed up in the first posted.

//I’m using window to run wampserver,phpmyadmin version 3.4.5

Jump out of follow the book mode for a short while. Isolate this inability to correctly locate or write your includes from the rest of your book, get to the bottom of the problem, solve it and then go back.

Try this:

Create a file called test_include.php

test_include.php


<?php
echo 'You have found the test file!';
?>

Then create a file called test.php

test.php


<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

include 'test_include.php';

?>

Now put those 2 files in the same folder, load up test.php and tell us if the message ‘You have found the test file!’ appears.

If it does, then move the test_include file to your include folder, then keep messing with the path using the suggestions already given above …


include 'test_include.php';  // add things to this path - hardcode them at first eg /Wamp/www/includes/test_include.php

until you get the success message back again.

Then take a good hard look at the manual page on includes, and decide on how you are going to handle includes initially.

Come back and ask here if you are unsure what the terminology means, or where the settings are best made. There are many options.

Thanks to you, Cups. I did tried what you said, its working, after that I inspired by your idea, and i found out that i didn’t call the function, that’s why my browser blank. The book is correct, just it didn’t wrote the code for calling the function, i followed blindly and i thought it was the problem of directory files, actually its a careless mistake. Now i feel happy and proceed with my lesson =D !

I don’t know if it matters but, as an advice, instead of $_SERVER[‘DOCUMENT_ROOT’] you may use dirname(FILE).‘/’ as a relative reference.
The document_root may be “far away” from your file so, it’s safer to refer to the current file path.
Good luck!

what’s the difference between dirname(FILE) and $_SERVER[‘DOCUMENT_ROOT’]? one more, how to use dirname(FILE)? give me an example please, thanks!

let’s say we have:


index.php
[lib]
---[classes]
------class1.php
------class2.php
---config.php

To require [root]/lib/classes/class1.php from [root]/lib/config.php we can have:

  1. require dirname(FILE).‘/classes/class1.php’;
  2. require $_SERVER[‘DOCUMENT_ROOT’].‘/lib/classes/class1.php’;
  3. require ‘classes/class1.php’;

First, 3 is not OK (worst case) because if our “config.php” is required by another file, from another folder, the path will not be valid anymore.
Let’s say that in [root]/lib/classes/class1.php we have require ‘…/config.php’; the final content of the file (class1.php + the include) will be:

<?php
require '../config.php';
// -- and this will produce
require 'classes/class1.php'; // (from our config.php)
// -- fatal error: we try to require [root]/lib/classes/classes/class1.php - not here!

  1. and 2) use absolute server path so you cannot fail BUT if the website is moved into a folder (it’s a strange case, but it can happen - the server is changed and the DOCUMENT_ROOT is not the same or you need a [beta] folder of the website), we’ll have:

FOLDER
---index.php
------[lib]
---------[classes]
------------class1.php
------------class2.php
------config.php

In case (2), instead of
$_SERVER[‘DOCUMENT_ROOT’].‘/lib/classes/class1.php’;
we’ll need
$_SERVER[‘DOCUMENT_ROOT’].‘/FOLDER/lib/classes/class1.php’;

In the (1)st case everything it’s compatible.
No changes are needed. It’s just a better practice that I would suggest.

i got it for the case(3), but i got problem on case(1). When i tried <?php include_once $_SERVER[‘DOCUMENT_ROOT’].‘/includes/magicquotes.inc.php’;, it’s working. After that, i tried it with <?php include_once dirname(FILE).‘/includes/magicquotes.inc.php’;, it isn’t working. I got an a notice that said "Use of undefined constant FILE “, and 2 warnings which are " include_once(./includes/magicquotes.inc.php) [function.include-once]: failed to open stream: No such file or directory in C:\wamp\www\jokes-helpers\index.php on line 2" and " include_once() [function.include]: Failed opening ‘./includes/magicquotes.inc.php’ for inclusion (include_path=‘.;C:\php\pear’) in C:\wamp\www\jokes-helpers\index.php on line 2

Anyproblem with dirname(FILE) or typing error?

use FILE not FILE :slight_smile: http://php.net/manual/en/language.constants.predefined.php
Edit: didn’t know that DIR appeared since 5.3 so, you may use it if you have PHP 5.3

Thats OK, glad I was able to help.

Learning from books is really great, but you should get comfortable with giving yourself permission (and time) for diving out of the book and isolating and testing some of the principles for yourself.

Eventually you will find that development follows the same path, lots of little bits of code that you already know work - joined up together.

Accepting that writing more code will simply mean you will make more mistakes (at least when starting out) is hard to swallow, but once you start to think about it you can adopt several practices to code more defensively, and your mind should be full of questions like:

“I just wrote this and that, which means that variable $x should contain ‘xxxx’ - how can I prove that worked?”

Some times all it takes is a simple echo or var_dump() (this will be your best friend EVER) and sometimes you should isolate the problem, such as was your case.

Thanks for your advice. I got it, always think but not follow blindly, haha!