Codeigniter - use two like and where together - php

I have a problem with use two like statement and where together:
$this->db->select('something');
$this->db->where('WHERE',$var);
$this->db->where_in('WHEREIN', $var);
$this->db->like('LIKE1',$query);
$this->db->or_like('LIKE2',$query);
$query = $this->db->get('table');
My query must select LIKE1 or LIKE2 where WHERE andWHEREIN is true.
If I use or_like, where statement get or too,
If i use just like, it's become like AND like statement
Any solution??

I found this solution:
use group_start() and group_end(), so my code turn to
$this->db->select('something');
$this->db->where('WHERE',$var);
$this->db->where_in('WHEREIN', $var);
$this->db->group_start();
$this->db->like('LIKE1',$query);
$this->db->or_like('LIKE2',$query);
$this->db->group_end();
$query = $this->db->get('table');

If you have a complex where clause, you can write it this way:
$this->db->where("doc = '123456' AND place IN('1,2,3') AND name (LIKE '%query%' ESCAPE '!' OR code LIKE '%query%' ESCAPE '!' )", NULL);

When you run this kind of query is better if you create a model like this:
function example(){
$query = "( SQL QUERY )";
$search = $this->db->query($query);
return $search->result_array();
}

Related

CodeIgniter Model Function, getting synatx error

I have written one codeigniter model function, but getting syntax error in php 7.. will you please suggest me changes
Error near group by order by statements..
My Code Snippet:
function listing($searchText = '', $page, $segment)
{
$this->db->select('tr.*,group_concat(tg.groundname) as grplist ');
$this->db->from('tb_tournament as tr ');
$this->db->join('tournamentground as tg', 'tr.tournamentId=tg.tournamentId', 'left');
$this->db->where('tr.is_deleted','0');
if(!empty($searchText)) {
$likeCriteria = " (tr.organizerName LIKE '%".$searchText."%'
OR tr.location LIKE '%".$searchText."%'
OR tr.phone LIKE '%".$searchText."%'
OR tr.email LIKE '%".$searchText."%'
OR tr.level LIKE '%".$searchText."%'
OR tr.gender LIKE '%".$searchText."%')";
$this->db->where($likeCriteria);
}
$this->db->group_by('tr.tournamentId');
$this->db->order_by('tr.tournamentId', 'DESC');
$this->db->limit($page, $segment);
$query = $this->db->get();
$result = $query->result();
//print"<pre>";
//print_r($result);
return $result;
}
You can use following logic in your query:
....
$this->db->or_like('tr.organizerName',$searchText);
$this->db->or_like('tr.location',$searchText);
....
....
It's done :)
The error is with the SQL query being generated, not from PHP.
Instead of this big where condition you need need to use query grouping as outlined in https://www.codeigniter.com/user_guide/database/query_builder.html#query-grouping
$likeCriteria = " (tr.organizerName LIKE '%".$searchText."%'
OR tr.location LIKE '%".$searchText."%'
OR tr.phone LIKE '%".$searchText."%'
OR tr.email LIKE '%".$searchText."%'
OR tr.level LIKE '%".$searchText."%'
OR tr.gender LIKE '%".$searchText."%')";
$this->db->where($likeCriteria);
I haven't actually used codeIgnitor, but based on a quick look at the docs you probably need to replace the problematic block with something like this:
$this->db->group_start()
->or_where(tr.organizerName LIKE "%{$searchText}%")
->or_where(tr.location LIKE "%{$searchText}%")
->or_where(tr.phone LIKE "%{$searchText}%")
->or_where(tr.email LIKE "%{$searchText}%")
->or_where(tr.level LIKE "%{$searchText}%")
->or_where(tr.gender LIKE "%{$searchText}%")
->group_end();
It's worth noting that you could use some business logic to improve this query as well. For instance you could recognise when the search term is a phone number, or an email and restrict the query accordingly
here is my solution with query grouping
function listing($searchText = '', $page, $segment)
{
$this->db->select('tr.*,group_concat(tg.groundname) as grplist ');
$this->db->from('tb_tournament as tr ');
$this->db->join('tournamentground as tg', 'tr.tournamentId=tg.tournamentId', 'left');
$this->db->where('tr.is_deleted','0');
if(!empty($searchText)) {
$this->db->group_start()
$this->db->like('tr.organizerName',$searchText);
$this->db->or_like('tr.location',$searchText);
$thsi->db->group_end()
}
$this->db->group_by('tr.tournamentId');
$this->db->order_by('tr.tournamentId', 'DESC');
$this->db->limit($page, $segment);
$query = $this->db->get();
$result = $query->result();
//print"<pre>";
//print_r($result);
return $result;
}
Read more about query grouping here
https://www.codeigniter.com/user_guide/database/query_builder.html#query-grouping

I need the output of this $LoadId=implode(',',array_filter($_POST["load"])); to look like this ('7209','7210')

SO i get data from a form using this
$LoadId=implode(',',array_filter($_POST["load"]));
I then would like to submit this to a MSSQL query with an "in" statement
where myLoadId in $LoadId
but the $LoadID looks like 7209,7210 and I need it to look like
('7209','7210')
Seems your LoadId column contains interger value so why you need single quotes ' around it? Simply use-
$LoadId=implode(',',array_filter($_POST["load"]));
$query = "SELECT * FROM your_table WHERE myLoadId IN ($LoadId)";
echo $query;
If you still need quotes around it then you can do it this way-
$LoadId = "'".implode("','", array_filter($_POST["load"]))."'";
$query = "SELECT * FROM your_table WHERE myLoadId IN ($LoadId)";
echo $query;
WORKING DEMO: https://3v4l.org/2XEjJ
Put simple quotes around the implode() and change it's glue from , to ',' :
$LoadId = "'".implode("','", array_filter($_POST["load"]))."'";

codeigniter : searching string with quotes( ' or " ) showing error

i am try to implement a search that but it shows error when I use Single Quotes like (manu's ,ramu's)
When I change my term part like %".$term."% and use back quotes it shows same error.
Query :
$this->db->select('*');
$this->db->from('tbl_doctor');
$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
$this->db->where("(tbl_doctor.dr_name LIKE `%".$term."%` OR tbl_doctor.district LIKE `%".$term."%` OR tbl_specialisation.spec_specialise LIKE `%".$term."%`OR tbl_doctor.place LIKE `%".$term."%` )");
$this->db->limit($limit, $offset);
and my error
You need escape string before including it inside query string. Use $this->db->escape_like_str() to escape string.
As you asked "why?", here is the explanation.
Explanation : When you are trying to add anu's in your search query its single quote(') is getting treated as end of string. escape_like_str() will automatically add slash() before any quote to prevent string from unintended termination. Escape That's why you need to escape the string before adding it inside your query.
$this->db->select('*');
$this->db->from('tbl_doctor');
$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
$this->db->where("(tbl_doctor.dr_name LIKE '%".$this->db->escape_like_str($term)."%' OR tbl_doctor.district LIKE '%".$this->db->escape_like_str($term)."%' OR tbl_specialisation.spec_specialise LIKE '%".$this->db->escape_like_str($term)."%'OR tbl_doctor.place LIKE '%".$this->db->escape_like_str($term)."%' )");
$this->db->limit($limit, $offset);
You need to escape the string. Use $this->db->escape_like_str().
Try this code :
$this->db->escape_like_str($term);
You need to use Escape String to search. Try this below code with codeigniter function
$this->db->escape_like_str($term)
New Code is
$this->db->select('*');
$this->db->from('tbl_doctor');
$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
$this->db->where("(tbl_doctor.dr_name LIKE `%".$this->db->escape_like_str($term)."%` OR tbl_doctor.district LIKE `%".$this->db->escape_like_str($term)."%` OR tbl_specialisation.spec_specialise LIKE `%".$this->db->escape_like_str($term)."%`OR tbl_doctor.place LIKE `%".$this->db->escape_like_str($term)."%` )");
$this->db->limit($limit, $offset);
Wrap $term variable with mysql_real_escape_string() in query.
Like : mysql_real_escape_string($term)
Source

Split mysql query and delete a part of that

I have many conditions in PHP function which every of them produces a mysql query.All conditions work correctly except one query which ends with AND operator.Before returning the query result I need to check if query ends with AND it should remove AND and then returnes the query.
This is the sample of query:
$query="select * from case where case_name='name' AND case_status='102' AND";
If this kind of query is produced I need to do:
1-If it ends with AND
2-remove AND
3-return the query without last AND
The result should be like this:
$query="select * from case where case_name='name' AND case_status='102' ";
I do not have much experience to work with PHP functions.How can I do this?
Thnaks for your help.
Try this,
$query="select * from case where case_name='name' AND case_status='102' AND"
$query = trim($query,'AND');
quick fix:
$query = preg_replace( "/AND$/", "", $query);
You should fix the logic of condition though.
like
$cond[] = "....";
$cond[] = "...."
....
then
$query = $query_first_half + implode ( " AND " , $cond );
Ultimately please use sql library like PDO
http://fi1.php.net/manual/en/class.pdo.php
explode the string and pop the last element .
$arr = explode(" ", $query);
$last = array_pop($arr);
if($last != "and")
{
array_push($arr,$last);
}
$query = implode(" ",$arr);
Run the $query them it should work
First your table name CASE is mysql reserved keyword you should rename your table to something else or escpae it by backticks `
you could use query without AND , and when you add other query just start by AND .
like that :
$query="select * from `case` where case_name='name' AND case_status='102'";
$query .= " AND .........";
so like that , your condition is not true then just first query will work , if condition is true then second query will work and it start by AND. You dont need to remove the AND.

Perform sql operations for this special case

Sir,
I am facing a problem and it is shown below:
I want to perform where opertion on the output of $this->db->select('*')->from('table'); only when cond1 satisfies
$result = $this->db->select('*')->from('table');
if(cond1)
{
I want to perform $result->where(); operation
}
Is it possible and i so what is the exact syntax to do it
try something like this:
$this->db->select('*');
$this->db->from('table');
if ($cond1)
$this->db->where(...)
$query = $this->db->get();
$con1 is should be a variable and it should has a value or Null. $con1 like a Boolean.1 or 0 .if 1(true) happening it'll execute the if block if not it'll execute the else block.
Looks like you want to run two different queries. Assuming that $cond1 is not dependent upon $result (in which case all bets are off), you can do something like
if(cond1)
$result = $this->db->select('*')->from('table')->where(...);
else
$result = $this->db->select('*')->from('table');

Categories