0

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);

}

Edited by rpv_sen: code change

2
Contributors
5
Replies
28
Views
2 Years
Discussion Span
Last Post by rpv_sen
1

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;

Edited by pritaeas

0

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

Edited by rpv_sen: update

0

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 ?

0

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.

0

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;
    }

Edited by rpv_sen: update line number for reference

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.