• Page:
  • 1

Passing Array into Mysql IN Query

ONLINE

Passing Array into Mysql IN Query

1 week, 2 days ago
In Primefocus

When i want to pass ids which have got in array into IN query

Example:these is the array of ids that have got


array(5) {
  [0]=>
  string(4) "7463"
  [1]=>
  string(4) "7462"
  [2]=>
  string(4) "7461"
  [3]=>
  string(4) "7460"
  [4]=>
  string(4) "7459"
}



To Work like :


SELECT * FROM #__pft_travelreimbursement where id in('7463','7462','7461','7460','7459');



so i used below query


$travelid = "'" . implode( "|", $ids) . ".*";

$travelid .= "'";

$query = "SELECT * FROM #__pft_travelreimbursement where id REGEXP ($travelid)";




Result:-


SELECT * FROM #__pft_travelreimbursement where id REGEXP ('7463|7462|7461|7460|7459.*')



This will exactly work like IN query

Please try this and let me know if there is any sugesstions
The following user(s) said Thank You: Adarsh Surania, Rajat Pal, Ramratan Singh
ONLINE

Re: Passing Array into Mysql IN Query

1 week, 2 days ago
Hi Sagar,

Its a good find but If you just want to implode the array with the single quotation. You can simply use the below code -

$temp = array("7463","7462","7461","7460");
$result = "'" . implode ( "', '", $temp ) . "'";


Now you can simply put the $result in query as follows -

$query = "SELECT * FROM #__pft_travelreimbursement where id IN ($result)";


Instead of using REGEXP in query and implode the array on pipe(|) and make it a regular expression. Above mentioned code would be more simple.

Let me know if you face any issue in this.

Thanks
The following user(s) said Thank You: Sagar S Vemul
  • Page:
  • 1

Upcoming Events

Who's Online