简体   繁体   中英

CodeIgniter SQL Query to include apostrophes and search every column in a specific order

First off, I'm not a programmer and attempting to learn from Google on a project I've been trying to complete for myself.

Technologies Used: CodeIgniter 2.1.2, PHP 5.3, MySQL

I have a search keyword box on my page, what I'd like this to do is search an entire table but show results in a specific order.

I currently have this SQL query which does not work with apostrophes and does not order the results by column.

function search($keyword)
 {
  $query="SELECT *
  FROM HWC
  WHERE ModelName LIKE '%{$keyword}%' OR Toy LIKE '%{$keyword}%'OR Year LIKE '%{$keyword}%'
  OR Col LIKE '%{$keyword}%'OR Series LIKE '%{$keyword}%'OR Color LIKE '%{$keyword}%'
  OR Tampo LIKE '%{$keyword}%'OR BaseColor LIKE '%{$keyword}%'OR BaseType LIKE '%{$keyword}%'
  OR WindowColor LIKE '%{$keyword}%'OR InteriorColor LIKE '%{$keyword}%'OR WheelType LIKE '%{$keyword}%'
  OR Country LIKE '%{$keyword}%'OR 'Series#' LIKE '%{$keyword}%'OR Notes LIKE '%{$keyword}%'";

  $result=$this->db->query($query);
  $result=$result->result_array();

  return $result;
}

I know this is wrong, it was temporary to get some things working.

Honestly I could break it into 3 catagories for sorting

ModelName
Toy
Other (All the rest)

I'd also like the SQL Query that I build to support pagination, but that shouldn't be hard with an offset and limit from what I read.

You might have not notice but there is an error in your query year is your column name and mysql also has a function year so it will generate the error you have put you column name with back ticks " ` " if your are not sure that it will conflict with t any function name, Best practice to add the back ticks on all columns so there will be no chance of conflicting so it will look like this

SELECT *
  FROM HWC
  WHERE `ModelName` LIKE '%{$keyword}%' OR `Toy` LIKE '%{$keyword}%' OR `Year` LIKE '%{$keyword}%'
  OR `Col` LIKE '%{$keyword}%'OR `Series` LIKE '%{$keyword}%' OR `Color` LIKE '%{$keyword}%' 
  OR `Tampo` LIKE '%{$keyword}%' OR `BaseColor` LIKE '%{$keyword}%' OR `BaseType` LIKE '%{$keyword}%'
  OR `WindowColor` LIKE '%{$keyword}%' OR `InteriorColor` LIKE '%{$keyword}%' OR `WheelType` LIKE '%{$keyword}%'
  OR `Country` LIKE '%{$keyword}%' OR `Series#` LIKE '%{$keyword}%' OR `Notes` LIKE '%{$keyword}% '

For order by the result set you can order by with multiple columns like

ORDER BY col1,col2,col3

For pagination of the result you have use the LIMIT function of mysql with the starting index and the ending index like

LIMIT 0 , 10 or for first 10 results simply you can specify LIMIT 10 keep in mine that limit will be applied at the end of query by default it will order the result set in ascending order but you can specify the order if you want result set in descending order DESC .

SELECT *
  FROM HWC
  WHERE `ModelName` LIKE '%{$keyword}%' OR `Toy` LIKE '%{$keyword}%' OR `Year` LIKE '%{$keyword}%'
  OR `Col` LIKE '%{$keyword}%'OR `Series` LIKE '%{$keyword}%' OR `Color` LIKE '%{$keyword}%' 
  OR `Tampo` LIKE '%{$keyword}%' OR `BaseColor` LIKE '%{$keyword}%' OR `BaseType` LIKE '%{$keyword}%'
  OR `WindowColor` LIKE '%{$keyword}%' OR `InteriorColor` LIKE '%{$keyword}%' OR `WheelType` LIKE '%{$keyword}%'
  OR `Country` LIKE '%{$keyword}%' OR `Series#` LIKE '%{$keyword}%' OR `Notes` LIKE '%{$keyword}% '
ORDER BY `ModelName`, `Toy` LIMIT 0,10'

Note

If you are not adding the back ticks on the column names then make sure they are unique and also put a space between the operators, functions etc and the column names like LIKE '%{$keyword}%'OR should be with space LIKE '%{$keyword}%' OR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM