0

Sometimes I get a fatal error in this line

$result = $db->execute($query);

$primary = $result->getRows();

Fatal error: Call to a member function getRows() on a non-object

I'm searching for a solution to catch this error and redirect to index page.

Rewriting something is complicated, because the system is old.

5
  • Don't try catch this, check if $result is an object/not boolean. Commented May 29, 2013 at 14:15
  • Possible duplicate: stackoverflow.com/questions/277224/… Commented May 29, 2013 at 14:19
  • 1
    not exactly, if the error can be prevented, it doesn't need to be catched Commented May 29, 2013 at 14:19
  • What driver is used for $db? Commented May 29, 2013 at 14:20
  • @DaveChen php.net/manual/en/pdostatement.getrows.php Commented May 29, 2013 at 14:38

6 Answers 6

2

Something like this:

$result = $db->execute($query);

if ($result===false) {
    header("Location: errorpage.php");
    exit;
}
$primary = $result->getRows();
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for using a guard clause instead of putting all your success logic inside a giant if statement.
I can't use header because get warning that header already send
Please make sure that nothing else is echo'd or displayed on the page. I wouldn't recommend, but you could use the ob_*, or echo javascript to redirect.
The best method is of course NOT having any content before header.
1

In the first place, there shouldn't be an error either way.

You have to fix your code to correct the error, instead of writing new code to handle it.

Get the error message out of DB to see the certain problem that caused this error and fix it.

Also

  • You cannot catch a fatal error in general.
  • You shouldn't redirect anywhere. In case of error a '503 HTTP header' have to be returned, along with generic 503 page.

Comments

0

The problem here is that execute() will return FALSE under some circumstances and a boolean is not the result object.

To avoid this error you should always be checking the output of execute() if it was even executed successfully.

For example:

$result = $db->execute($query);

if ($result !=== false) {
 $primary = $result->getRows();
}

Comments

0

We need to know what $db is, you have not included the code where $db is initialised or informed us as to what this references.

Assuming you are using the PDO library, the PDO object itself has PDO::exec() and the PDOStatement object has PDOStatement::execute(). I therefore assume $db is an instance of PDOStatement and you are calling the execute() method, which returns a boolean, false upon a failure and true upon success - it does not return an object upon success. If successful, you should then call $db->fetchAll();

if (!$db->execute($query)) {
    header("Location: error.php");
    exit;
} 

$rows = $db->fetchAll();

Comments

0

Normally you can't catch an error... only exceptions.... luckily with PHP you can set up an error_handler that does throw exceptions. (from comments on php ErrorException page)

class ErrorHandler extends Exception {
    protected $severity;

    public function __construct($message, $code, $severity, $filename, $lineno) {
        $this->message = $message;
        $this->code = $code;
        $this->severity = $severity;
        $this->file = $filename;
        $this->line = $lineno;
    }

    public function getSeverity() {
        return $this->severity;
    }
}

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorHandler($errstr, 0, $errno, $errfile, $errline);
}

set_error_handler("exception_error_handler", E_ALL);

So for your specific problem you would just do:

try{
    $result = $db->execute($query);
    $primary = $result->getRows();
} catch (ErrorHandler $e){
    header("Location: /index.php");
    exit;
}

1 Comment

Just saying, your class is not a "handler", maybe you should change its name. There's a built-in exception that does exactly this: br.php.net/errorexception
-1

If you get the error message "header already sent" it's because something is already outputted. For instance:

echo "hello world;";
header("Location: errorpage.php");

or:

 <?php // notice the space before <?php 
header("Location: errorpage.php");
?>

Comments

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.