Jump to content

How do I fix this?


rizmah

Recommended Posts

So I am hosting from 000webhost and when I goto my index.php it shows a blank white screen. I view the source of the screen and it said 

Fatal error:  Call to a member function fetchColumn() on a non-object in /home/*REMOVED*/public_html/staff/header.php on line 2

So I check this line of code and it's...

<title>  <?php echo $odb->query("SELECT `Tab` FROM `SiteConfig` LIMIT 1")->fetchColumn(0); ?> </title>

I don't know how to fix it? I am using phpmyadmin too and have siteconfig in there.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/291126-how-do-i-fix-this/
Share on other sites

Most likely your query is failing. The error is telling you that the method fetchColumn() is trying to be executed against a non-object. In that line of code there is "$odb->query()" being used to execute a query and then "->fetchColumn(0)" is attempting to be executed against that result. If the query fails there is nothing for fetchColumn() to execute against.

 

You have no error handling in that code. IMO, you should never assume queries will pass. You should always include error handling to at least provide the user some feedback. Although you would never want to dump raw error messages to the user in a production environment, just do something such as "a problem occurred, please try again later". But, you can either check for those errors and log them or echo them out in a non-prod environment. I also don't think that running "code" within HTML is a good idea, makes maintainability more difficult.

 

 

<?php
    $result = $odb->query("SELECT `Tab` FROM `SiteConfig` LIMIT 1");
    if(!$result)
    {
        $title = "Unable to retrieve title";
        //Add additional error handling to get the real error info
    }
    else
    {
        $title = $result->fetchColumn(0);
    }
?>
 
<title><?php echo $title; ?></title>
Link to comment
https://forums.phpfreaks.com/topic/291126-how-do-i-fix-this/#findComment-1491395
Share on other sites

 

Most likely your query is failing. The error is telling you that the method fetchColumn() is trying to be executed against a non-object. In that line of code there is "$odb->query()" being used to execute a query and then "->fetchColumn(0)" is attempting to be executed against that result. If the query fails there is nothing for fetchColumn() to execute against.

 

You have no error handling in that code. IMO, you should never assume queries will pass. You should always include error handling to at least provide the user some feedback. Although you would never want to dump raw error messages to the user in a production environment, just do something such as "a problem occurred, please try again later". But, you can either check for those errors and log them or echo them out in a non-prod environment. I also don't think that running "code" within HTML is a good idea, makes maintainability more difficult.

<?php
    $result = $odb->query("SELECT `Tab` FROM `SiteConfig` LIMIT 1");
    if(!$result)
    {
        $title = "Unable to retrieve title";
        //Add additional error handling to get the real error info
    }
    else
    {
        $title = $result->fetchColumn(0);
    }
?>
 
<title><?php echo $title; ?></title>

 

I didn't make this code, im editing to fit my needs.

I tried your code, still white screen.

P.S. I'm a newb.

Link to comment
https://forums.phpfreaks.com/topic/291126-how-do-i-fix-this/#findComment-1491398
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.