Welcome Guest, Not a member yet? Register   Sign In
CI3 num_rows on CI4
#1

In CI3 I did this


PHP Code:
$query $this->db->select('fileds')->where('id'123)->get('table');

$this->row $query->row();

$this->doesExist $query->num_rows(); 


In CI4, on the other hand, I tried these ways


PHP Code:
$query $this->db->table('table')->select('fileds')->where('id'123)->get();

$this->row $query->getRow();

$this->doesExist $query->countAllResults(); 


or

PHP Code:
$this->doesExist $query->countAll(); 


or

PHP Code:
$this->doesExist $query->getNumRows(); 


The query gets the data, but getting the number of rows doesn't work in any way
Reply
#2

Try this:

PHP Code:
public function number_of_something($something_id){
        $db      = \Config\Database::connect();
        $builder $db->table('something_table as st');
        $query $builder->select('*')
                 ->where('st.something_id'$something_id);

 return 
$query->countAllResults();
    
Reply
#3

If there is only one row expected, just do:

PHP Code:
$row $this->db->table('table')->select('fileds')->where('id'123)->get()->getRow();


if(
$row) {
 
// Got result

Reply
#4

It has changed.

PHP Code:
$query    $db->query('SELECT * FROM my_table');
$rowCount $query->getNumRows(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(03-27-2022, 12:05 AM)InsiteFX Wrote: It has changed.

PHP Code:
$query    $db->query('SELECT * FROM my_table');
$rowCount $query->getNumRows(); 

I tried that too, but VS Code keeps reporting it to me as an error
Reply
#6

(This post was last modified: 03-29-2022, 06:10 AM by wdeda.)

PHP Code:
$db $this->db// inside a model
$db db_connect(); //out of a model

$count $db->table('your_table');
echo 
$count->countAllResults(); 
If plan A fails, relax... the alphabet is 26 letters
Reply
#7

(03-29-2022, 06:09 AM)wdeda Wrote:
PHP Code:
$db $this->db// inside a model
$db db_connect(); //out of a model

$count $db->table('your_table');
echo 
$count->countAllResults(); 

It is not a query problem, the quest is performed correctly, I just want to know / understand which of the methods I listed at the beginning I have to use instead of the old num_rows, because whatever I put is marked as an error by the VS Code parser, which does not happen with any other function linked to the DB
Reply
#8

What do you mean by VS Code parser? Are you using an extension that hooks into CI4?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#9

If it doe's not work then you must be using a different version of CodeIgniter 4

system/Database/BaseResult.php it's there in CodeIgniter 4.1.9

PHP Code:
$query    $db->query('SELECT * FROM my_table');
$rowCount $query->getNumRows(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

(This post was last modified: 03-30-2022, 01:45 AM by serialkiller.)

(03-29-2022, 09:57 AM)ignitedcms Wrote: What do you mean by VS Code parser? Are you using an extension that hooks into CI4?

I use extensions, but only one related to php which until now has always reported non-existent functions that's why I don't understand

(03-30-2022, 12:03 AM)InsiteFX Wrote: If it doe's not work then you must be using a different version of CodeIgniter 4

system/Database/BaseResult.php it's there in CodeIgniter 4.1.9

PHP Code:
$query    $db->query('SELECT * FROM my_table');
$rowCount $query->getNumRows(); 

Yes, in fact I searched and found myself too, at this point the problem must be on the sequence of the code, probably in this way it is not recognized as an executable or something like that, I will post the relae code
Reply




Theme © iAndrew 2016 - Forum software by © MyBB