create a mysql query with using php arrays -


i have , arrray below

  $titles=array("dr.","ms.","mr."); 

after foreach loop, create query below

select * `table` title = 'dr.' or title = 'ms.' or title = 'mr.' ) group sentences 

output start

mr. ms. mr. dr.

i want put dr. first ms. mr. in order inside of array()

as long input sanitized, can use implode prepare statement.

$stmt = 'select * `table` '; $stmt .= "where title = '" . implode("' or title = '", $titles) . "'"; 

result

select * `table` title = 'dr.' or title = 'ms.' or title = 'mr.' 

see demo

you can alternatively use in:

$stmt = 'select * `table` '; $stmt .= "where title in ('" . implode("', '", $titles) . "')"; 

result

select * `table` title in ('dr.', 'ms.', 'mr.') 

you need fix group by; you're not using correctly.

if want sort order of titles, can try:

order   case     when title = 'dr' 1     when title = 'ms.' 2     when title = 'mr.' 3     else 4   end 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -