Home > other >  Using PHP in MYSQL for order by with MariaDb
Using PHP in MYSQL for order by with MariaDb

Time:08-24

I'm trying to retrieve records in the order in which I think they are being accessed.

My code is as follows:

the data to search for:

$to_check="Yes_ij_affirmation', ',_cm', 'there_px_ex', 'is_vbz_1', '._fs";

the select statement:

$sql = "SELECT wd, wd_ps2, rt1, rt4, definition FROM august_2022 WHERE wd_ps2 IN ($to_check) order by ".$to_check."";

and the outcome is (I've put it in list form to make it easier to see v the original):

  • Yes_ij_affirmation
  • there_px_ex
  • ._fs (should be last)
  • ,_cm (should be second)
  • is_vbz_1 (should be third)

I'm not sure whether what I am trying to do is feasible, but would welcome advice.

WW

CodePudding user response:

You need quotes at the beginning and end of $to_check.

Then you can use the FIELD() function to order by the position in that list.

$to_check="'Yes_ij_affirmation', ',_cm', 'there_px_ex', 'is_vbz_1', '._fs'";

$sql = "SELECT wd, wd_ps2, rt1, rt4, definition 
        FROM august_2022 
        WHERE wd_ps2 IN ($to_check) 
        order by FIELD(wd_ps2, $to_check)"

CodePudding user response:

You need to add quotes in

$to_check

$to_check="'Yes_ij_affirmation', ',_cm', 'there_px_ex', 'is_vbz_1', '._fs'";

And the select statement should look like this:

$sql = "SELECT wd, wd_ps2, rt1, rt4, definition FROM august_2022 WHERE wd_ps2 IN ($to_check) order by " .$to_check;
  • Related