Hi I am trying to display count of a row using function with get values. Please help me to fix the issue

class

class Users {

        private $conn;
        private $db_name= "DATABASE";

        public function __construct($db)
            {
                $this->conn = $db;
            }

            /*Join Two Tables with where condition and order by*/
        public function getJoinRows($tablen1,$tablen2,$where,$order_by){
            $query = $this->conn->prepare("select * FROM $this->db_name.$tablen1,$this->db_name.$tablen2 where $where ORDER BY $order_by DESC");
            try {
                    $query->execute();

                    return $query;

            }catch(PDOException $e) {
                    die($e->getMessage());
                }
        }

/**/
        public function transCount($tablen,$whereCondition)
        {
            $query = $this->conn->prepare("select count(*) from $this->db_name.$tablen where $whereCondition");
            try{
                $query->execute();
                return $row_count = $query->fetchAll();
            }catch(PDOException $e) {
                    die($e->getMessage());
                }
        }
}

Todisplay

$Users = new Users($db);
$query = $Users->getJoinRows('FQ64004 a','FQ64005 b','a.SSYQ64UKID=b.SSYQ64UKID','a.SSYQ64LRDT,a.SSYQ64TJBS');                                                      
$i=1;
while($row_stream = $query->fetch(PDO::FETCH_ASSOC)) {

extract($row_stream);

$Users = new Users($db); 
$query = $Users->transCount('FQ64003','SEYQ64SID=$SSYQ64SID'); 
echo $count($query);

}

This line returns an array of rows containing the count:

return $row_count = $query->fetchAll();

What you should return is the first entry in that array:

$row = $query->fetchAll();
return $row[0][0]; // first row, first column

Since you only retrieve one row, this is better IMO:

$row_count = 0;
if ($row = $query->fetch())
{
    $row_count = $row[0];
}
return $row_count;

Apart from that, I think this line:

$query = $Users->transCount('FQ64003','SEYQ64SID=$SSYQ64SID');

should be:

$query = $Users->transCount('FQ64003', "SEYQ64SID='$SSYQ64SID'"); 

And:

echo $count($query);

should be:

echo $query;

Update of Todisplay

$Users = new Users($db);
    $query = $Users->getJoinRows('FQ64004 a','FQ64005 b','a.SSYQ64UKID=b.SSYQ64UKID','a.SSYQ64LRDT,a.SSYQ64TJBS');
    $i=1;
    while($row_stream = $query->fetch(PDO::FETCH_ASSOC)) 
    {
        extract($row_stream);

        $Users = new Users($db); 
        $query = $Users->transCount('FQ64003', "SEYQ64SID='$SSYQ64SID'"); 
        echo $query;
    }

i am getting Fatal error: Call to a member function fetch() on a non-object in line 4

i am getting Fatal error: Call to a member function fetch() on a non-object in line 4

Hmz. Shouldn't you be returning the result object, instead of $query ?

Thanks pritaeas

If i remove line 8 to 10. It will display all the values except number of rows.

If i use 8 to 10 i am getting the issue.

I fixed the issue.

Final code of Todisplay

I changed $query to $uquery in line 2 and 4

$Users = new Users($db);
    $uquery = $Users->getJoinRows('FQ64004 a','FQ64005 b','a.SSYQ64UKID=b.SSYQ64UKID','a.SSYQ64LRDT,a.SSYQ64TJBS');
    $i=1;
    while($row_stream = $uquery->fetch(PDO::FETCH_ASSOC)) 
    {
        extract($row_stream);

        $query = $Users->transCount('FQ64003', "SEYQ64SID='$SSYQ64SID'"); 
        echo $query;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.