troubleshooting Question

PDO Fatal error: Call to a member function prepare() on a non-object in

Avatar of imask
imask asked on
3 Comments1 Solution3501 ViewsLast Modified:
I am getting Fatal error: Call to a member function prepare() on a non-object in dbh.

define("VERSION", "1.0");
define("MYSQLUSER", "");
define("MYSQLPASS", "");
define("MYSQLDB", "db");

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null : define('SITE_ROOT', $_SERVER['DOCUMENT_ROOT']);
defined('LIB_PATH') ? null : define('LIB_PATH', $_SERVER['SCRIPT_FILENAME']);
global $dbh;

function _init( )

   // connect to the database (persistent)
   try {
        $CRUD['dbh'] = new PDO('mysql:host=localhost;dbname=' . MYSQLDB, MYSQLUSER, MYSQLPASS, array( PDO::ATTR_PERSISTENT => true ));

    } catch (PDOException $e) {

function error( $s )

function error_message( $s = "" )
//    global $CRUD;
    if($s) $CRUD["_ERR_ARRAY"][] = "<p class=\"error_message\">$s</p>\n";
    return isset($CRUD["_ERR_ARRAY"]) ? $CRUD["_ERR_ARRAY"] : array();

function get_sitemap_sql()

	$dbh = $CRUD['dbh'];
    $query = " SELECT html, TitleTag FROM content WHERE html <> 'sitemap'";
    $sth = $dbh->prepare($query);
    if($sth) $sth->execute();
    else error('get_sitemap_sql: select prepare returned no statement handle');

    $err = $sth->errorInfo();
    if($err[0] != 0) error( $err[2] );


// START CODE ==============================================


  $sth = get_sitemap_sql();  
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros