2

I have a query, how do I get the result as an array?

function get_all_transaksi_proses() {
    $rs = $this->db->query("SELECT a.id_transaksi,
                                   a.nama,
                                   a.tgl_transaksi, 
                                   (SELECT COUNT( id_transaksi ) AS jum  
                                     FROM  tbl_detail_trs_menu  
                                     WHERE id_transaksi = a.id_transaksi)  AS jumlah, 
                                   a.status_transaksi, 
                                   a.total, 
                                   b.status_pelanggan, 
                                   c.nama_karyawan 

                            FROM   tbl_transaksi a 

                                   LEFT JOIN  tbl_pelanggan b 
                                      ON a.id_pelanggan = b.id_pelanggan 

                                   LEFT JOIN tbl_karyawan c 
                                      ON a.id_karyawan = c.id_karyawan 

                            WHERE  a.status_transaksi =  'PROSES' ");  

  echo json_encode(array("result" => $rs)); 
}

3 Answers 3

5

Like this:

$rs = $this->db->query(...);
$array = $rs->result_array();

https://www.codeigniter.com/user_guide/database/results.html

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

8 Comments

Message: preg_match() expects parameter 2 to be string, object given
@user3012773 eh? there's no preg_match() in the code that you're showing. is that somewhere else in your code? don't think it has anything to do with this question.
but i think this error from my query, because I run the query directly on routes codeigniter.. can help me
That error should also tell you which php file and what line number its coming from. Without seeing all of your code its impossible for me to know.
function index() { $this->model_transaksi->get_all_transaksi_proses(); }
|
2

To get a result in array you have to do this CI 2.2.2:

    return $query->result_array();

Comments

1
$query = $this->db->query("select....");

return $query->result(); // it will result an array
or
echo json_encode($query->result()); // this will also result array but direct to json_encode

....hope i help you!

Comments

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.