problem with fetch

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mostafa272
Forum Newbie
Posts: 3
Joined: Tue Apr 17, 2012 1:21 am

problem with fetch

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: problem with fetch

Post by requinix »

And what query are you trying to run?
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: problem with fetch

Post 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
Post Reply