简体   繁体   中英

how do i Select from table with WHERE condition and desc order using query builder function?

I'm trying to select comments in my Comments table. And I want to select them based on a Report Number and in descending order using the codeigniter 3 query builder functions, how can I do it?

I got this code here in my model and if I var_dump it just doesn't work the way I want to. this is the code I have so far:

    $this->db->order_by('Date', 'DESC');
    $query=$this->db->where("Comments", array('ReportNum' => $report));
    $row=$query->result();

this what i get when i var_dump it:

object(CI_DB_mysqli_result)#27 (8) { ["conn_id"]=> object(mysqli)#16 (18) { ["affected_rows"]=> int(5) ["client_info"]=> string(13) "mysqlnd 8.0.9" ["client_version"]=> int(80009) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(5) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(21) "5.5.5-10.4.20-MariaDB" ["server_version"]=> int(100420) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(571) ["warning_count"]=> int(0) } ["result_id"]=> object(mysqli_result)#26 (5) { ["current_field"]=> int(0) ["field_count"]=> int(5) ["lengths"]=> NULL ["num_rows"]=> int(5) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(5) { [0]=> object(stdClass)#28 (5 )

^ this comes before the desired array.

You need a get clause to query a table, you can either use:

where():

$this->db->order_by('Date', 'DESC');
$this->db->where( array('ReportNum' => $report));
$query=$this->db->get("Comments");
$row=$query->result();

or using get_where():

$this->db->order_by('Date', 'DESC');
$query=$this->db->get_where("Comments", array('ReportNum' => $report));
$row=$query->result();

about CI 3.x query builder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM