I want to run a query like this:
SELECT * FROM table WHERE field LIKE '%search_term%'
In CI you can bind parameters to queries, if you used field=? but this does not work for field LIKE "%?%". From debugging output it seems the query used is field LIKE "%'search'%".
Is there an alternative way to do searching in CodeIgniter?
You can use this query:
SELECT * FROM table WHERE field LIKE ?
And bind with %search% instead of search.
You should be aware that this query will be slow in MySQL. You might want to look at free-text search instead (Lucene, Sphinx, or MySQL's built-in free-text search functions).
this is active record class for codeigniter
$this->db->select('*');
$this->db->like('columnname','both');
$query=$this->db->get("tablesname");
$result=$query->result_array();
if(count($result))
{
return $result;
}
else
{
return FALSE;
}
You should try this..I think its help you..
both means %columnname%,
before means %columnname,
after means columnname%
$search_term=$this->input->post('textboxName');
$search_term="%".$search_term."%";
$sql="SELECT * FROM table WHERE field LIKE ? ";
$query=$this->db->query($sql,array($search_term));
$res=$query->result();
what i can understand CI is adding quotes, pass FALSE as third parameter while binding to prevent CI adding quotes.
Related
i am working on a table that includes a filter function.
For the filter i use a form where i enter the parameters.
Those are added to a string which is my SQL query.
So far it works fine.
There is oine input field where multiple parameters canbe added.
The plan is to seperate them with ; .
For example 520;521;522
My plan was to use str_replace to convert this in to sql Code.
For example
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
But some how this code does not show the expected results.
I only get results for '%520%'
How do i need to adjust this query in order to have the sql query working?
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
In another input field i search for names.
Here the query looks like this...
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (Bearbeiter LIKE '%Heine%' OR Bearbeiter LIKE '%Wolf%' OR Bearbeiter LIKE '%Maiwald%')
This works fine!
The multiple like should be written as,
SELECT *
FROM MaschinenVorgangslisteMitHV
WHERE VorgangNr REGEXP '520|522|523';
I believe you need to add VorgangNr LIKE after every OR.
Please first read my question,and then you will find out it is not a duplicate of other question.
I'm using sphinx search for 98% of search,but need to use match against for just one query.
As we know from mysql documentation that AGAINST only takes string.The search string must be a literal string, not a variable or a column name.
But I have found this link http://bugs.mysql.com/bug.php?id=66573 ,which says it is possible.But I'm not sure how to use that in my case.
Here is my code
$sqli="SELECT busi_title,category FROM `user`.`user_det`";
$queryi=mysqli_query($connecti,$sqli);
if(mysqli_num_rows($queryi)>0){
while($rowi=mysqli_fetch_assoc($queryi)){
$busi_title=$rowi['busi_title'];
$category=$rowi['category'];
}
}else{
echo "OH NO";
}
$sqlj="SELECT * FROM `user`.`user_det` WHERE MATCH(student) AGAINST('$busi_title','$category')";
$queryj=mysqli_query($connecti,$sqlj);
if(mysqli_num_rows($queryj)>0){
..............................
..............................
}else{
foreach ( $res["matches"] as $doc => $docinfo ) {
................................
...............................
}
}
MATCH() AGAINST() is giving error,as it supposed to be.How to use that trick of that link in this case.I don't know the use of #word:= of that link.
Thanks in advance.
That link doesn't show a trick to get around a limitation of MySQL. It's a bug report demonstrating an incorrect statement in the MySQL documentation. The statement in the documentation has now been corrected.
The reason you're getting an error is because you're sending two parameters to AGAINST and it only accepts one. You can use a MySQL variable in AGAINST which is what the bug report is about, but this has nothing to do with the PHP variable that you're using.
EDIT
Upon reading your response, I rather suspect that you have your syntax backwards.
SELECT * FROM `user`.`user_dets` WHERE MATCH(busi_title, category) AGAINST('student')
But note this from the documentation:
The MATCH() column list must match exactly the column list in some FULLTEXT index definition for the table, unless this MATCH() is IN BOOLEAN MODE. Boolean-mode searches can be done on nonindexed columns, although they are likely to be slow.
If you don't have a Fulltext index, you'll actually want this:
SELECT * FROM `user`.`user_dets` WHERE `busi_title` LIKE '%student%' OR `category` LIKE '%student%'
When they say "The search string must be a literal string, not a variable or a column name" does not mean you cannot use variable to create your Query String.
So it is OK to make your query very simple.
Your WHERE could be this:
WHERE `student` = $busi_title OR `student` = $category
I am working on a problem where I need to add an OR clause to a set of existing conditions. The current conditions are built in a hash in a method and at the end, they are used in the where clause. Here is a simplified example:
...
conds.merge!({:users => {:archived => false}})
Model.where(conds)
I am trying to add an OR clause to the current set of conditions so it would be something like '(conditions) OR new_condition'. I'd like to add the OR statement without converting each addition to the conds hash into a string. That would be my last option. I was hoping someone has done something like this before (without using Arel). I seem to recall in Rails 2 there was a way to parse a conditions hash using a method from the model (something like Model.some_method(conds) would produce the where clause string. Maybe that would be a good option to just add the OR clause on to that string. Any ideas are appreciated. Thank you for your help!
I found a way to do what I needed. Instead of changing all of the conditions that I am building, I am parsing the conditions to SQL using sanitize_sql_for_conditions. This is a private method in ActiveRecord, so I had to put a method on the model to allow me to access it. Here is my model method:
def self.convert_conditions_hash_to_sql(conditions)
self.sanitize_sql_for_conditions(conditions)
end
So, once I convert my conditions to text, I can add my OR clause (along with the appropriate parentheses) to the end of the original conditions. So, it would go something like this:
Model.where('(?) OR (model.type = ? AND model.id IN(?))', Model.convert_conditions_hash_to_sql(conds), model_type, model_id_array)
I fairly new to YII and still trying to understand it all. However from what I can tell when you do something like
yourModel->findAll(criteria)
Is like "Select * from"? or is it more like "Select yourModel->Attributes from"? In either case I was wondering in CDbCriteria is there a way to remove columns from the select. My case I have a user table that contains password I would like to prevent this from being added in the query.
Thanks,
Ofcourse you can select specific columns, just use the select property of CDbCriteria:
$criteria=new CDbCriteria();
$criteria->select='column1, column2';// or you can use array array('column1','column2')
$manymodels=$yourmodel->findAll($criteria);
So it is more like : "Select criteria->select from yourmodelclass' dbtable".
Note that findAll() will return you an array of models.
I'm using Rails 3 with a MySQL database, and I need to programmatically create a query like this:
select * from table where category_name like '%category_name_1%'
OR category_name like '%category_name_2%'
(...snip...)
OR category_name like '%category_name_n%'
Given the table size and the project scope (500 rows at most, I think), I feel that using something like thinking sphinx would be overkill.
I know I could simply do this by writing the query string directly, but wanted to know if there's an ActiveRecord way to do this. There's no mention of this on the official guide, and I've been googling for a long while now, just to end empty-handed :(
Also, is there a reason (maybe a Rails reason?) to not to include the OR clause?
Thanks!
Assuming you have an array names with category names:
Model.where( names.map{"category_name LIKE ?"}.join(" OR "),
*names.map{|n| "%#{n}%" } )
you should google first, there is already an answer.
Look here and then here
and you'll get something like this:
accounts = Account.arel_table
Account.where(accounts[:name].matches("%#{user_name}%").or(accounts[:name].matches("%#{user_name2}%")))
If you look at the guide, they have examples that can easily be modified to this:
Client.where("orders_count = ? OR locked = ?", params[:orders], false)
Mysql has a regexp function now that can clean things up a bit, assuming there's no regex metachars in your category names:
Table.where "category_name regexp '#{names.join('|')}'"