Page 1 of 1

problem with fetch

Posted: Sun Apr 22, 2012 11:34 pm
by mostafa272
Hi

I want to make a CMS,but in my first test,I recieved following error:

Fatal error: Call to a member function fetch() on a non-object in C:\wamp\www\cms\ww.incs\basics.php on line 23

basic.php:

Code: Select all

  <?phpsession_start();
function __autoload($name) {
    require $name . '.php';
}
function dbInit(){
    if(isset($GLOBALS['db']))return $GLOBALS['db'];
    global $DBVARS;
    $db=new PDO('mysql:host='.$DBVARS['hostname'].';dbname='.$DBVARS['db_name'],$DBVARS['username'],$DBVARS['password']);
    $db->query('SET NAMES utf8');
    $db->num_queries=0;
    $GLOBALS['db']=$db;
    return $db;
}
function dbQuery($query){
    $db=dbInit();
    $q=$db->query($query);
    $db->num_queries++;
    return $q;
}
function dbRow($query) {
    $q = dbQuery($query);
    return $q->fetch(PDO::FETCH_ASSOC);
}
define('SCRIPTBASE', $_SERVER['DOCUMENT_ROOT'].'cms/' );
require SCRIPTBASE . '.private/config.php';
if(!defined('CONFIG_FILE'))define('CONFIG_FILE',SC  RIPTBASE.'.private/config.php');
set_include_path(SCRIPTBASE.'ww.php_classes'.PATH_  SEPARATOR.get_include_path());  
I use wampserver in localhost, php_pdo and php_pdo_mysql extensions are enable! please help me.
Thanks

Re: problem with fetch

Posted: Mon Apr 23, 2012 12:22 am
by requinix
And what query are you trying to run?

Re: problem with fetch

Posted: Mon Apr 23, 2012 12:54 pm
by x_mutatis_mutandis_x
Set the PDO to run in exception mode

Code: Select all

function dbInit() {
    ...
    $db=new PDO('mysql:host='.$DBVARS['hostname'].';dbname='.$DBVARS['db_name'],$DBVARS['username'],$DBVARS['password'], array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    ...
}
After you do that let us know if you get a different error, or still the same one