Home  >  Q&A  >  body text

Implement data sorting in MariaDb using PHP and MYSQL

I'm trying to retrieve the records in the order I think they were accessed.

My code is as follows:

Data to be searched:

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

Select statement:

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

The result is (I put it in a list to make it easier to see the comparison to the original result):

I'm not sure if what I'm trying to do is possible, but suggestions are welcome.

WW

P粉085689707P粉085689707222 days ago524

reply all(2)I'll reply

  • P粉638343995

    P粉6383439952024-04-04 00:33:21

    You need to add quotes in the code

    $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;

    reply
    0
  • P粉131455722

    P粉1314557222024-04-04 00:11:43

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

    You can then use the FIELD() function to sort by position in the 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)"

    reply
    0
  • Cancelreply