Call to undefined method CodeIgniter\Database\MySQLi\Builder::num_rows()

Viewed 4500

Im trying to make a code generator for my table

Here's my model :

public function buat_kode()   {

    $this->db->table('RIGHT(proses_cutting.kode_packing_list,6) as kode', FALSE);
    $this->builder()->orderBy('kode_packing_list','DESC');    
    $this->builder()->limit(1);    
      $query = $this->db->table('proses_cutting');      //cek dulu apakah ada sudah ada kode di tabel.    
      if ($query->num_rows() <> 0){      
       //jika kode ternyata sudah ada.      
        $data = $query->row();      
        $kode = intval($data->kode) + 1;    
      }
      else {      
       //jika kode belum ada      
        $kode = 1;    
      }

      $kodemax = str_pad($kode, 6, "0", STR_PAD_LEFT); // angka 4 menunjukkan jumlah digit angka 0
      $kodejadi = "PLIK059".$kodemax;    // hasilnya ODJ-9921-0001 dst.
      return $kodejadi;
    }

controller :

public function create()
{
    session();
    $data = [
        'main' => 'prosescutting/create',
        'validation' => \Config\Services::validation(),
        'title' => 'Form Tambah Proses Cutting',
        'kodeunik' => $this->pcuttingModel->buat_kode(),
        'kode_packing_list' => $kodePackingSekarang,
    ];
    return view('template/template', $data);
}

and my table : Table

What im trying to do is make a generator for kode_packing_list for each item of nama_barang so when i input another Renata Blouse it will automatically fill with PLIK059000007

But when I tried to run it, it show me these errors:

Error Call to undefined method CodeIgniter\Database\MySQLi\Builder::num_rows() APPPATH\Models\PcuttingModel.php at line 33

and the line 33 is

if ($query->num_rows() <> 0){  
2 Answers

You are using the wrong database table functions, try this:

$db = \Config\Database::connect(); // optional; init database if not created yet

$builder = $db->table('proses_cutting');
$builder->select('RIGHT(proses_cutting.kode_packing_list,6) as kode');
$builder->orderBy('kode_packing_list','DESC');
$builder->limit(1);

if($builder->countAllResults() > 0) {
    $query = $builder->get();
    $result = $query->getResult(); // Result as objects eg; $result->kode
    $kode = $result->kode;
}

// Other option: I believe you can do this too

$builder = $db->table('proses_cutting');
$builder->select('RIGHT(proses_cutting.kode_packing_list,6) as kode');
$builder->orderBy('kode_packing_list','DESC');
$builder->limit(1);

$query = $builder->get();
$result = $query->getResult('array');
if(is_array($result) && count($array) > 0) {
    $kode = $result['kode'];
}

For more information about the builder, see the CI documentation: https://codeigniter4.github.io/userguide/database/query_builder.html

@SukmaQintara I think this is what you will do. Codeigniter 4 query builder does not really have that method num_row declared but us countResultAll()

Example

      $query = $this->db->table('proses_cutting');      //cek dulu apakah ada sudah ada kode di tabel.    
      if ($query->countResultAll() <> 0){      
       //jika kode ternyata sudah ada.      
        $data = $query->row();      
        $kode = intval($data->kode) + 1;    
      }
      else {      
       //jika kode belum ada      
        $kode = 1;    
      }

If it does not work try my other answerMy last answer OR use the reference document Check this Document

Related