1
//List of id's I want displayname for
$IDListSQL = '10,10,10,11,10,10';
//My query statement
$q = "SELECT displayname FROM accounts WHERE id IN($IDListSQL)";
//Executes query
$Res = $DB->Query($q);
//Gets all results
$Rows = $Res->fetch_all();
echo var_dump($Rows);
//Output
#->array(2) 
{ 
   [0]=> array(1) 
   { 
       [0]=> string(14) "Apple" 
   } 
   [1]=> array(1) 
   { 
       [0]=> string(10) "Orange" 
   }
}

The behaviour I want is an array with all the displayname's in the $IDListSQL, even if its the same ID. I also want them in the order I specified. Trying to figure this out in 1 query rather than doing up to 16 separate select queries for the purpose this is for. Any help would be appreciated kindly.

  • I was downvoted, most likely partially due to that you are not using prepared statements in your PHP code. Start with that, and then worry about the query. – Tim Biegeleisen Feb 13 '18 at 02:22
  • IN wont give you what you want...start here - https://stackoverflow.com/questions/11835155/mysql-split-comma-separated-string-into-temp-table...you need to split the string into a table (youll need a column for the order too)...then youll JOIN that temp table with your data – Ctznkane525 Feb 13 '18 at 02:31
  • Whether or not it is a prepared statement isn't the problem here. When I run it on my SQL console it gives me the same result PHP is getting. The problem is the query here.. The query you gave me did not solve my problem. – user3166623 Feb 13 '18 at 02:35

1 Answers1

0

I ended up getting this done with PHP since I already had an array of ID's in the specified order. Used my same query to only get one of each ID, then joined the data into my array with the help of a couple for loops. Appreciate the help, Ctznkane525 I think what you posted would work. It sounds like it is doing the same thing I done up in PHP, trying not to use complex queries unless absolutely necessary. Speed and high ccu is critical for this app.