I need to query (read) some data, and analyze it before performing another query (read) on a different table.
I've looked at mysqli multiple statements but they don't have documentation for situations where the second query depends on the result of the first query.
It seems my current code of doing two queries might not be optimal. Is there a more optimal way to do this?
//FIRST QUERY
$query1= "SELECT color FROM products WHERE type = '$productType';";
$result = $conn->query($query1);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$colorProduct = $row["color"];
}
}
//ANALYZE SEARCH RESULT FROM FIRST QUERY
if ($colorProduct == "green") {
$colorType = "greenColorType";
}
//a lot more analysis
//SECOND QUERY
$query2 = "SELECT price FROM Vendors WHERE color = '$colorType';";
$result2 = $conn->query($query2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
$priceOfProduct = priceOfProduct . $row["price"];
}
}
$conn->close();
Thank you in advance.
.~ presumably this is for addition so you would probably want+instead?$colorTypeis only set if$colorProduct == "green". Are you sure that this is what you want? Perhaps state explicitly what you're trying to achieve, as your code is difficult to interpret in it's current state.