I have an API that checks against a SQL database for a part number, but there is a case where there are multiple part numbers for a SKU and when it gets that result it doesn't return anything in the UI, is there a way to choose one? From the results? For now I had this
$squery ="SELECT part_no FROM parts where cb_master ='".$sku."'";
$res = mysqli_query($connectiondb, $squery);
$i=0;
$row = mysqli_fetch_assoc($resultado);
$part = $row['part_no'];
if(is_array($row)){
$check=0;
while($check=1){
$fixQuery = "SELECT no_item FROM orderdet WHERE no_order = $order AND no_item = $part[$i]";
$arrayPart=mysqli_query( $connectiondb, $fixQuery);
$i++;
if($arrayPart!=''){
$parte=$arrayPart;
$check=1;
return $part;
}
}
}
I had a "limit 1" in the query but it didn't work, I don't know how to choose a single result (any of them would work), any ideas or help would be appreciated.
If you only need to retrieve one of the records when multiple matches exist, you can use LIMIT 1 in the query and retrieve the first result with mysqli_fetch_assoc() . However, there are several problems in your code: you're using different variables ( $res and $resultado ), the while($check=1) condition performs an assignment instead of a comparison, and $part is not an array but a single value.
If there are indeed multiple part_no results, you should iterate through all of them using mysqli_fetch_assoc() within a while loop and decide which one to use based on your business logic. The problem seems to lie more in the handling of the results than in the SQL query itself.