6 replies
I am getting the following error.

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

i have attached the code. What could be causing this issue. it works find in php4 but when i change to 5 i get that error

PHP Code:
if(!defined('SITE_PATH')) die('Hacking attempt');

function 
pear_session_open($save_path$name)
{
  return 
true;
}

function 
pear_session_close()
{
  return 
true;
}    

function 
pear_session_read($session_id)
{
  global 
$db;
  
$sql "SELECT data FROM " TBL_SESSION " WHERE session_id = '"
       
addslashes($session_id) . "'";
  return 
$db->one_data($sql);
}

function 
pear_session_write($session_id$data)
{
  global 
$db;
  
$expires HW_TIME get_cfg_var("session.gc_maxlifetime");
  
$sql "REPLACE INTO " TBL_SESSION "(session_id, expires, data) "
       
"VALUES('" addslashes($session_id) . "', $expires, '" addslashes($data)
       . 
"')";
  
$db->query($sql);
  return 
true;
}

function 
pear_session_destroy($session_id)
{
  global 
$db;
  
$sql "DELETE FROM " TBL_SESSION " WHERE session_id = '"
       
addslashes($session_id) . "'";
  
$db->query($sql);
  return 
true;
}

function 
pear_session_gc($gc_maxlifetime)
{
  global 
$db;
  
$db->query("DELETE FROM " TBL_SESSION ' WHERE expires < '.HW_TIME);
  
// $db->query('OPTIMIZE TABLE '.TBL_SESSION);
  
return true;
}

session_set_save_handler(
    
"pear_session_open",
    
"pear_session_close",
    
"pear_session_read",
    
"pear_session_write",
    
"pear_session_destroy",
    
"pear_session_gc"); 
#error #php
  • Profile picture of the author K Meier
    You have 3 functions that use "query". To which one of the 3 functions does your error message relate to?
    {{ DiscussionBoard.errors[4066789].message }}
  • Profile picture of the author KirkMcD
    You need to find where the global variable db is first initialized. You are probably getting an warning there that you are ignoring.
    {{ DiscussionBoard.errors[4067033].message }}
  • Profile picture of the author layouts4you
    what i dont get is it works in php4 but when we change to php5 it does this.
    {{ DiscussionBoard.errors[4076752].message }}
  • Profile picture of the author ussher
    are you sure that $db has been defined?

    might need to be defined. try re-calling it to check if it is actually available where you want it.

    So at the top do
    $db = new WhateverThatClassIs();

    If it works then, then you know that its not being defined earlier in the script and you can go looking for why.
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[4077211].message }}
    • Profile picture of the author layouts4you
      yes its defined. it works 100% in php4. this goes to my login page. for some reason in php5 it dies and will not login and gives that error.
      {{ DiscussionBoard.errors[4077790].message }}
  • Profile picture of the author Terry Crim
    php4 and php5 are different, work differently and have built-in functions and features that do not exist in the other. Also what version of php 5? 5.2 is different than 5.3, in 5.3 there are things that have been depreciated or no longer work that did in 5.2.

    The error you pasted:
    PHP Fatal error: Call to a member function query() on a non-object

    Means that the database connection doesn't exist, is not being passed to $db or was never connected in the first place.

    Also from a programming standpoint it is better to specifically pass the database call to the function instead of using a global variable. It is just better programming practice that way.
    {{ DiscussionBoard.errors[4078255].message }}

Trending Topics