0

I am running into a blank page, and although I have told PHP to report all errors I still get nothing which leads me to believe it must be a syntax error. I can't find what it is though.

Here is the script I am working on:

test.php

<?php
ini_set('display_errors', '1');
require('database.php');

print("hello");

$config = new Config("lessons.db","data/");
$db = new Database($config, array('first', 'second', 'third', 'fourth'), true);

print_r($db->dumpToArray());
?>

database.php

<?php
    class Config {
        private
            $_db
            $_file,
            $_directory,
            $_delimiter,
            $_columns;

        public function __construct(string $file, string $directory = null, string $delimiter = "|")  {
            $_db = $directory.$file;
            $_directory = $directory;
            $_delimiter = $delimiter;
        } 
        public function db() {
            return $_db;
        }
        public function delimiter() {
            return $_delimiter;
        }

    }       
    class Database {
        private
            $_config,
            $_columns,
            $_rows,
            $_pointer;

        public function __construct(Config $config, array $constants = null, boolean $caseInsensitive = false)  {
            $_config = $config;
            is_readable ($_config->db())
                or exit ("The database cannot be read");
            if(!is_null($constants))
                $this->defineColumns($constants, $caseInsensitive);
            return;
        } 

        private function connect(string $method) {
            if (!($_pointer = @fopen($_config->db(), $method)) or printf("Unable to connect to database");
        }

        private function disconnect() {
            fclose($_pointer);
        }

        private function defineColumns($constants, $caseInsensitive) {
            for (var $i=0;$i<count($constants);$i++)
                define($constants[i], $i, $caseInsensitive);                
        }

        public function dumpToArray() {
            $arrayDump = explode($_config->delimiter(), file($_config->db(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
            return $arrayDump;
        }

        public function getRowByValue($column, $value) {
            $this->connect('r');
            $rowNumber = 0;
            while (!feof($_Pointer)) {
                $row = explode($_config->delimiter(), fgets($dbPointer, 1024));
                if ($row[$column] == $value) {
                    $this->disconnect();
                    return $row;
                }
                $rowNumber++;
            }
            $this->disconnect();
            return false;
        }       
    }
?>

Anyone can see what could be causing it?

6
  • I'd try php -l <filename> just to make sure it's not a syntax issue first. Commented Dec 6, 2011 at 14:28
  • @Flukey don't have access to them, I am on a strange corporate set up. Commented Dec 6, 2011 at 14:29
  • @hafichuk Do I need to modify that code? Never come across it. Commented Dec 6, 2011 at 14:30
  • 1
    When I can't access the error logs (or when the problem is unexpected behaviour rather than an actual error) and I don't have access to a debugger, I divide and conquer. Simply comment out the major section of code you think is causing the problem. Once you have the problem broadly localized, then start uncommenting blocks of code until the problem recurs. Commented Dec 6, 2011 at 14:37
  • Voting to close this question as too localized one. Asking people to find syntax errors for you is above the limits. Commented Dec 6, 2011 at 14:58

6 Answers 6

1

I think require(database.php); should be require('database.php');.

Try changing that and see if it helps

Also, you're missing a semicolon on

return $arrayDump

EDIT

Okay, I'm not too sure, but try to remove the casting from the parameters of the functions.

So...

public function __construct(Config $config, array $constants = null, boolean $caseInsensitive = false) 

Would be

public function __construct($config, $constants = null, $caseInsensitive = false)  {

I don't do much OOC in PHP, but just taking another shot.

Sign up to request clarification or add additional context in comments.

5 Comments

I just found a missing semicolon
wow thanks mr eagle eyes, still white screening though. Updated source.
I'm not sure, but you may want to check the last line of the __construct function in the Database class. I may be wrong, but it's not returning anything
I didn't think you had to but I may be wrong. Although even if I set it to return true; it still white screens =/
I found a way to get error reporting on a different server and that was one of the problems along with a missing colon and bracket. I will accept your answer for finding the most errors :)
1

At first glance... This won't fly

$db = new Database($config, ["first","second","third","fourth"], true);

Your Database class expects an array

$db = new Database($config, array('first', 'second', 'third', 'fourth'), true);

5 Comments

I think he was wanting to make an array
@jprofitt You're right. I realized after reading his Database class.
Thanks, been doing a lot of actionscript and got the syntax muddled there +1 but it is still not functioning.
@George Please don't describe problems with general terms. "not functioning." Is it still white-screening? Is there a new error?
@MikeB Sorry it is still white screening - I updated post to match the source as it is currently
1

Anyone can see what could be causing it?

Thats most wrong and inefficient way of looking for errors.
you can stare in your code for ages and even ask other people to do it, yet without any success.

Why not to just read the exact error message?
You were on the right track, but gave up.

As Flukey said in the comments, you have to check error log, which by default is the ame as web-server error log.

Watching the actual errors is the only proper way to correct your code.

3 Comments

I normally would but in my current environment I am unable to, it may not be right but it's what I have to do right now.
Don't tell me you have no server available to run this code with error reporting.
Uploaded to another server (doh, duh!) and am working my way through the errors now ta.
1

Tip

If you have a syntax error and are in a situation where the only way you can change the error_reporting/display_errors settings is in the script itself, you can still make it work.

The trick is to make another script that has no syntax errors, and in that script set your config options, and then include the suspected bad script.

<?php
error_reporting(-1); // show all
ini_set('display_errors', 1);
require 'file_with_parse_error.php';

Still, you should generally be able to change these settings in php.ini or webserver config files. And there's should be an error log available to look at in any case.

Then just request the url for that new script in your web browser.

Comments

0

there are other error reporting levels in php.ini - you might want to look into them.

Comments

0

Since you're unable to look at error logs, try throwing a bunch of print statements in your code and see what line is killing your script. If it's killing in your database.php script, throw some prints in and there.

<?php
ini_set('display_errors', '1');
require('database.php');
print "Test1";
print("hello");
print "Test2";
$config = new Config("lessons.db","data/");
print "Test3";
$db = new Database($config, array('first', 'second', 'third', 'fourth'), true);
print "Test4";

print_r($db->dumpToArray());
?>

1 Comment

Why the downvote? This is a legitimate way to debug if you can't see any errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.