Implement data sorting in MariaDb using PHP and MYSQL
P粉085689707
P粉085689707 2024-04-03 12:44:47
0
2
509

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

  • Yes_ij_Confirm
  • there_px_ex
  • ._fs (should be the last one)
  • ,_cm (should be the second one)
  • is_vbz_1 (should be the third one)

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

WW

P粉085689707
P粉085689707

reply all(2)
P粉638343995

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;
P粉131455722

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)"
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!