Two script completing the same task, which is the best way?
$string = '1/2/3/4';

$change = str_replace('/',',',$string);

$result = $connection-&gt;query("SELECT id, filename FROM pages WHERE id IN($change) ORDER BY FIELD(id,$change)");

$new_string = "";
while($row = $result-&gt;fetch_assoc()) {
     $new_string .= $row['filename']."/";
}

$new_string = substr($new_string,0,-1);

echo $new_string;


Less looping - the match[] = $row was unnecessary IMO because you want a string back only...so you concatenate the file name with a / delimiter and then the last slash is removed with sub_str.

Updated the fixes including single query match.