hunge — 2013-01-25T09:53:01-05:00 — #1
//this is my index file on totaljokes-error( a folder name)
include_once $SERVER['DOCUMENTROOT'] . '/includes/db.inc.php';
$result=$pdo->query('SELECT COUNT(*) FROM joke');
catch (PDOException $e)
$error='Database error counting jokes!';
//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.
2ndmouse — 2013-01-25T10:03:07-05:00 — #2
include_once ($SERVER['DOCUMENTROOT'] . '/includes/db.inc.php');
Or use a relative path:
hunge — 2013-01-25T10:10:31-05:00 — #3
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. ~.~...
starlion — 2013-01-25T10:17:45-05:00 — #4
What's the value of $SERVER['DOCUMENTROOT'] ? You mentioned you were running this on Windows....
You could try including "./includes/db.inc.php" (forcing a relative reference with .)
hunge — 2013-01-25T10:31:02-05:00 — #5
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..
hunge — 2013-01-26T09:34:01-05:00 — #6
The values appeared is C:/wamp/www/ for $SERVER['DOCUMENTROOT'].
What I wrote was include_once $SERVER['DOCUMENTROOT'] . '/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
cups — 2013-01-26T14:35:53-05:00 — #7
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.
Create a file called test_include.php
echo 'You have found the test file!';
Then create a file called test.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.
hunge — 2013-01-28T07:29:21-05:00 — #8
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 !
vectorialpx — 2013-01-28T07:40:07-05:00 — #9
I don't know if it matters but, as an advice, instead of $SERVER['DOCUMENTROOT'] 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.
hunge — 2013-01-28T07:57:01-05:00 — #10
what's the difference between dirname(FILE) and $SERVER['DOCUMENTROOT']? one more, how to use dirname(FILE)? give me an example please, thanks!
vectorialpx — 2013-01-28T09:33:26-05:00 — #11
let's say we have:
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['DOCUMENTROOT'].'/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:
// -- 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:
In case (2), instead of
In the (1)st case everything it's compatible.
No changes are needed. It's just a better practice that I would suggest.
hunge — 2013-01-29T02:25:34-05:00 — #12
i got it for the case(3), but i got problem on case(1). When i tried <?php include_once $SERVER['DOCUMENTROOT'].'/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?
vectorialpx — 2013-01-29T03:14:45-05:00 — #13
use FILE not FILE 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
cups — 2013-01-29T03:41:34-05:00 — #14
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.
hunge — 2013-01-29T21:12:03-05:00 — #15
Thanks for your advice. I got it, always think but not follow blindly, haha!