How can run a Call query while getting output from a first select query?

Hi. I’m running a first query to get IDs of all operational vehicles through a select query. The query alone outputs about 100 IDs (vehicles). What I’m trying to do is that while it is outputting the IDs, it is also running a Call Query against a mysql procedure to check if the vehicle is available for a certain period. When I combine the second query, I only get the first ID output, rather than all the rest. Can someone please check what I’m doing wrong? Below is my code.

<?php

// Attempt MySQL server connection.
$link = mysqli_connect(“localhost”, “username”, “password”, “database”);

// Check connection
if($link === false){
die(“ERROR: Could not connect. " . mysqli_connect_error());
}else{
// Attempt select query execution
$sql = “SELECT * FROM inventory WHERE Active = ‘Y’ ORDER BY Rate”;
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$id = $row[‘ID’];
$sdate = “2019-04-17”;
$edate = “2019-04-22”;
// Run Call query to check if each ID is avaiable for the period.
$query = mysqli_query($link, “CALL isavailable(”.$id.”,’".$sdate."’,’".$edate."’)");
while($line = mysqli_fetch_array($query)){
echo $id."-".$line[‘Available’]."<br/>";
}

  	}			
  } else{
  	echo "No records matching your query were found.";
  }

} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
}

?>

This is extremely inefficient. You should never run SELECT queries inside of loops.

You can do this using one LEFT JOIN query that gets the data you want in the order that you want it.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service