Welcome Guest, Not a member yet? Register   Sign In
MYSQL / Query Builder Class help
#6

(This post was last modified: 08-06-2017, 11:22 AM by neoraj3.)

(08-05-2017, 11:45 PM)Wouter60 Wrote:
Quote:How do I do a SELECT query that queries columns in 2 tables?  eg SELECT t1.column1, table2.column2 from t1, t2  using Query Builder
If you know how to do this in plain MySQL, you'll know that you need a JOIN statement.

Example in query builder, with tables "posts" and "comments":
PHP Code:
$this->db
->select ('t1.title, t2.comment_date')
->
from('posts t1')
->
join('comments t2','t2.post_id = t1.id')
->
order_by('t2.comment_date','DESC');
$query $this->db->get();
if (
$query->num_rows() == 0) {
 
  return FALSE;
}
else {
 
  return $query->result();


To apply old style SQL, just do this:
PHP Code:
$sql "SELECT * FROM posts ORDER BY id";
$records $this->db->query($sql)->result();
//$records is an array of objects 


Ho Wouter60,

I took your advice so far.  Can you check the MVC I have below and let know whats still wrong? 


PHP Code:
MODEL::
<?
php
        
public function get_news_stats($id){

        
$this->db->select('news.title, news_rating.rating');
        
$this->db->from('news');
        
$this->db->join('news_rating''news_rating.rating_id = news.id');
        
$this->db->where('id'$id);
        
$query $this->db->get();

        if (
$query->num_rows() == 0) {
        return 
FALSE;
        }
        else {
        return 
$query->result();
        }

        }
?>

CONTROLLER::

<?php
      public 
function index3($id)
            {
            
$data['news_ratings2'] = $this->news_model->get_news_stats($id);
            
$data['title'] = 'NEWS STATS';/;

            
$this->load->view('templates/header'$data);
            
$this->load->view('news/index3'$data);
            
$this->load->view('templates/footer');
            }
?>


VIEW::



<?php foreach ($news_ratings2 as $x): ?>

        <?php echo $x['rating_id']; ?>
            <?php echo $x['id']; ?>



<?php endforeach; ?>
Reply


Messages In This Thread
MYSQL / Query Builder Class help - by neoraj3 - 08-05-2017, 12:23 PM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 04:46 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 11:25 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 04:47 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 11:22 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-13-2017, 06:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB