Reputation: 45
I have and arrray below
$titles=array("Dr.","Ms.","Mr.");
after foreach loop, i create the query below
SELECT * FROM `table` WHERE title = 'Dr.' or title = 'Ms.' or title = 'Mr.' ) group by sentences
OUTPUT start from
Mr. Ms. Mr. Dr.
I want to PUT the DR. first that Ms. than Mr. which is in order inside of the array()
Upvotes: 0
Views: 51
Reputation: 34054
As long as your input is sanitized, you can use implode
to prepare the statement.
$stmt = 'SELECT * FROM `table` ';
$stmt .= "WHERE title = '" . implode("' OR title = '", $titles) . "'";
Result
SELECT * FROM `table` WHERE title = 'Dr.' OR title = 'Ms.' OR title = 'Mr.'
You can alternatively use IN
:
$stmt = 'SELECT * FROM `table` ';
$stmt .= "WHERE title IN ('" . implode("', '", $titles) . "')";
Result
SELECT * FROM `table` WHERE title IN ('Dr.', 'Ms.', 'Mr.')
You need to fix your GROUP BY
; you're not using it correctly.
If you want to sort the order of your titles, you can try:
ORDER BY
CASE
WHEN title = 'Dr' THEN 1
WHEN title = 'Ms.' THEN 2
WHEN title = 'Mr.' THEN 3
ELSE 4
END
Upvotes: 4
Reputation: 3280
you can try in statement which is easy to read.
SELECT *
FROM `urls`
WHERE `id`
IN ( 1, 2, 4 )
ORDER BY `urls`.`id` ASC
LIMIT 0 , 30
Upvotes: 0