PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] "Call to undefined method CI_DB_mysqli" when executing sql join using codeigniter active record set

 February 22, 2022     activerecord, codeigniter, php, sql     No comments   

Issue

Following the code igniter docs when I try to execute the following sql query I get this exception ? Would apprecate some help to point out the issue with the query

$query = $this->db->join('price', 'price.itemId = shop.itemId', 'right');
var_dump($query->result_array());
if ($query->num_rows() > 0){

        return $query->result();
    }

To give some background I have two tables shop and price. price table has this primary key itemId which is a foreign key in the price table. I want to get all records in the pricetable along with an attribute called name which is in the shop table


Solution

When you use the Join statement with the codeigniter query builder you must tell it with what table you want to join with.

So your code would look something like this:

$query = $this->db->join('price', 'price.itemId = shop.itemId', 'right')
             ->get('shop');

return $query->result();

Also there's no need to check if there's any rows to actually return something, just return the result like I did here, and you'll have returned an empty array. That way your function always return the same thing in this case an array.

It's not really good to return null and array in the same function.

For more information on how the query builder works here's a link to a very extensive documentation.

https://codeigniter.com/userguide3/database/query_builder.html?highlight=query%20builder



Answered By - marcogmonteiro
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,207,342

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing