简体   繁体   中英

Echo output sql JOIN?

How should echo output following query?

My tables in MySQL database:

在此处输入图片说明

$query = $this->db->query('SELECT geo.order FROM Store_Information si JOIN Geography AS geo ON geo.id = si.id WHERE si.name LIKE "%' . $find1 . '%"');

My try not work:

if ($query->num_rows() > 0){
   foreach($query as $val) {
      $query_out = $query->row();
      echo $query_out->order . '<br>';
   }
}
else {
    echo '0';
}

What do I do?

You need to add ->result() in your foreach loop:

// from the docs
$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
    echo $row->title;
    echo $row->name;
    echo $row->body;
}

http://codeigniter.com/user_guide/database/results.html

You should read this guide to better understand how to use codeigniter query results.

basically this code can help:

if($query->num_rows() > 0){
    foreach($query->result_array() as $row) {
        var_dump($row);
    }
}

and don't forget to $query->free(); your result

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