I was just wondering if there was a better way to write this block of code in php. I have a string with values separated by commas. I need to prepare that string to pass it on to the query using IN clause.
$sql = "SELECT domain, items FROM rep_skills WHERE report_id = $id ";
$data = $conn->query($sql)->fetch_object();
while($data){
// can this be done in one line?
$array = explode(",",$data->items); // string here is 39,40,41
$items = implode("','",$array); // string here is 39','40','41
// end of block
$result = $conn->query("SELECT description
FROM ".$tables[$data->domain]."
WHERE item_id IN ('".$items."')");
$description = '';
while($row = $result->fetch_object()){
$details .= '- '.$row->description;
}
}
$records
come from? How can you be certain that it contains only comma-separated numbers? \$\endgroup\$JOIN
query instead? \$\endgroup\$domain
, and theitems
are stored as comma-separated lists rather than as a table. The bad schema design is forcing you to write bad code to query it. \$\endgroup\$