Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write codeigniter like query

The problem is that it displays all the rows from db instead of displaying only those rows which contain search keywords: Please help me.

public function search($data)
{

    $name = $data["name"];
    $surname = $data["surname"];

    $this->db->select('*');
    $this->db->from('workers');
    $this->db->like('name', '%' . $name . '%');
    $this->db->or_like('surname', '%' . $surname . '%');

    $query = $this->db->get();    
like image 887
EducateYourself Avatar asked May 20 '15 18:05

EducateYourself


People also ask

How do I run a query in CI?

Bookmark this question. Show activity on this post. $ENROLLEES = $this->load->database('ENROLLEES', TRUE); $ACCOUNTS = $this->load->database('ACCOUNTS', TRUE); $SELECT = "SELECT $ACCOUNTS.

What is query Builder in codeigniter?

CodeIgniter gives you access to a Query Builder class. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.

How use distinct in join query in codeigniter?

You can use below mentioned query. $query = $this->db->group_by('category_master. Category_Id,business_profile_details. Business_Id');


1 Answers

You wrote CI like query wrong way.Look the documentation how to write it properly

Your query should be like this.No need to add %. CI add them if you do not pass 3rd parameter of like function.

$this->db->select('*');
$this->db->from('workers');
$this->db->like('name', $name);
$this->db->or_like('surname', $surname);

$query = $this->db->get();

Better do with some valid data like this.

$this->db->select('*');
$this->db->from('workers');
if($name)
{
   $this->db->or_like('name', $name);
}
if($surname)
{
    $this->db->or_like('surname', $surname);
}
$query = $this->db->get();
like image 77
Shaiful Islam Avatar answered Oct 16 '22 08:10

Shaiful Islam