0

I am trying to execute this query from an active record script in CodeIgniter 4.

SELECT pres.db_pem, COUNT(*) AS Count FROM pres GROUP BY pres.db_pem;

I have try with

public function countall()
{   
    $this->prestasi->selectCount('*', 'cat_total');
    $this->prestasi->select("db_pem");
    $this->prestasi->groupBy('db_pem', 'ASC');
    return $this->prestasi->get()->getRowArray();
}

but I am only getting one row like this:

array(2) {
    ["cat_total"]=> string(1) "2"
    ["db_pem"]=> string(1) "1"
}

I am expecting multiple rows in the result set.

0

1 Answer 1

0

You are using the active records library in the old way, that's Codeigniter 3 syntax.

For codeigniter 4 you should use the documentation like so: https://codeigniter.com/user_guide/database/query_builder.html

For your case should be something like this:

public function countall()
{ 
    $db      = \Config\Database::connect();
    $builder = $db->table('pres');
    $builder->select('COUNT(*) as count);
    $builder->groupBy('db_pem');
    $builder->orderBy('db_pem', 'ASC');
    $query   = $builder->get();
    
    return $query->getResult();
}

Sign up to request clarification or add additional context in comments.

3 Comments

What about some Method Chaining? What about selectCount() as attempted in the asked question?
@mickmackusa The edit on the Question is invalid. I'm not sure you're using any prompt to create this or on your own. stackoverflow.com/revisions/77930923/4. In this case error message is important here.
@AbdullaNilam the question became a moving target because Vickel provided partial support. Feel free to roll back and fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.