简体   繁体   中英

running using xampp error on ubuntu : Fatal error: Call to a member function rowCount() on a non-object

I'm testing a program running well using xampp and Windows 7 , But when i'm upload to the server using Ubuntu ( mysql , php , apache using apt-get ) it's getting error

Fatal error: Call to a member function rowCount() on a non-object in /var/www/siarsip/class/user.php on line 56

Here the code snipped :

 function getAllUser(){ $query=$this->db1->query("SELECT a.user_id,a.username,a.password,a.NIP,a.role,b.category,a.input_date,a.last_update FROM users as a RIGHT JOIN user_categories as b ON a.role=b.usercat_id ORDER BY role"); $jml_data=$query->rowCount(); if($jml_data>=1){ $hasil=$query->fetchAll(); //line 56 return $hasil; }else{ return $jml_data; } } 

I've tried to change line 56 to :

if(!empty($query) AND $jml_data > 0) {

Still not working.

update : Using @cjriii code, i've update line 56 to using this :

if(is_object($query))

{ { code here }

}

Now there's no error, i've tried to login produces same error "Fatal error: Call to a member function rowCount() on a non-object in /var/www/siarsip/class/user.php on line 74"

  function loginUser($username,$password){ $password_md5=md5($password); $query=$this->db1->query("SELECT*FROM users WHERE username='$username' AND password='$password_md5'"); if(is_object($query)) { $hasil=$query->rowCount(); //line 74 return $hasil; } } 

I've insert the code again to line 74 if(is_object($query)) {

Produces no error. But now i cannot login using the username and password that usually works.

Need another advice bro..

Two things:

Test if $query is an object before calling a method on it. Thats' where the error comes from.

Second, instead of testing for !empty($query), try testing for is_object($query).

if(is_object($query))
{
    $jml_data = $query->rowCount();
    if($jml_data >= 1)
    {
        //rest of your code here
    }
    else
        return $jml_data;
}

Edit:

Please check the documention on empty(): Your call may be returning a valid value for empty to return false.

http://php.net/empty http://php.net/manual/en/function.is-object.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM