phpmysqlmariadb-10.4

Using PHP in MYSQL for order by with MariaDb


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):

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

WW


Solution

  • 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)"