Mysql multiple queries from one PHP file I'm running the below code to populate a HTML table using several calls to mysqli_query(). The below snippet only shows three however I'm running ten in my file for various other date columns and may need more over time. I've attached a file below showing the basic database layout and required output.



The required output is basically just a list of times from various columns

The below code works fine, however it is probably not the most efficient and other options like MySQL JOIN or UNION don't seem to work.

Do we have any another options to perform the desired output?

 $result = mysqli_query($con,"SELECT * FROM livescores WHERE actualstart &gt;= '$datefilter'");
 $resulthalf = mysqli_query($con,"SELECT * FROM livescores WHERE actualhalftime &gt;= '$datefilter'");
 $actualft = mysqli_query($con,"SELECT * FROM livescores WHERE actualft&gt;= '$datefilter'");

while($row = mysqli_fetch_array($result))
{
echo "&lt;tr&gt;";
echo "&lt;td&gt;" . $row['actualstart'] . "&lt;/td&gt;";
echo "&lt;td&gt;" . $row['League'] . "&lt;/td&gt;";
echo "&lt;td&gt;KO&lt;/td&gt;";
echo "&lt;td&gt;" . $row['HomeTeam'] .' v '. $row['AwayTeam'] . "&lt;/td&gt;";
echo "&lt;td&gt;&lt;/td&gt;";
echo "&lt;/tr&gt;";
}
while($row = mysqli_fetch_array($resulthalf))
{
echo "&lt;tr&gt;";
echo "&lt;td&gt;" . $row['actualhalftime'] . "&lt;/td&gt;";
echo "&lt;td&gt;" . $row['League'] . "&lt;/td&gt;";
echo "&lt;td&gt;HT&lt;/td&gt;";
echo "&lt;td&gt;" . $row['HomeTeam'] .' v '. $row['AwayTeam'] . "&lt;/td&gt;";
echo "&lt;td&gt;&lt;/td&gt;";
echo "&lt;/tr&gt;";
}
while($row = mysqli_fetch_array($actualft))
{
echo "&lt;tr&gt;";
echo "&lt;td&gt;" . $row['actualft'] . "&lt;/td&gt;";
echo "&lt;td&gt;" . $row['League'] . "&lt;/td&gt;";
echo "&lt;td&gt;FT&lt;/td&gt;";
echo "&lt;td&gt;" . $row['HomeTeam'] .' v '. $row['AwayTeam'] . "&lt;/td&gt;";
echo "&lt;td&gt;&lt;/td&gt;";
echo "&lt;/tr&gt;";


Many Thanks
I can't say how the queries should be built for the other 7 that you haven't posted, but I think I've identified enough commonalities in your design intent to exercise the D.R.Y. principle.

While it is always best to minimize total calls to the database, it seems you are struggling to package all of the queries into one.  I'll suggest implementing mysqli_multi_query() for your case with the noticeable benefit being maintainability.  I am assuming that your $datefilter value is 100% validated/secure and is safe to place into your queries (versus being a raw user input).  I should also note that if any of your queries experience a syntax error, the loop will break, the html table will close early, and you will see details about the erring query.

The following method can be modified to allow blank rows and/or group headings withing the html table -- just write that portion between the do{ and if($result... lines.

Untested Code:

// $datefilter MUST be validated/trustworthy as prepared statements are not in use.
$queries['KickOff']="SELECT actualstart AS `Time`,League,'KO' AS Label,HomeTeam,AwayTeam FROM livescores WHERE actualstart&gt;='$datefilter'";
$queries['HalfTime']="SELECT actualhalftime AS `Time`,League,'HT' AS Label,HomeTeam,AwayTeam FROM livescores WHERE actualhalftime&gt;='$datefilter'";
$queries['Fulltime']="SELECT actualft AS `Time`,League,'FT' AS Label,HomeTeam,AwayTeam FROM livescores WHERE actualft&gt;='$datefilter'";
// ... ~7 more queries entries here

echo "&lt;table&gt;";
if(mysqli_multi_query($mysqli,implode(';',$queries))){
    do{
        if($result=mysqli_store_result($mysqli)){
            while($row=mysqli_fetch_assoc($result)){
                echo "&lt;tr&gt;&lt;td&gt;{$row['Time']}&lt;/td&gt;&lt;td&gt;{$row['League']}&lt;/td&gt;&lt;td&gt;{$row['Label}&lt;/td&gt;&lt;td&gt;{$row['HomeTeam']} v {$row['AwayTeam']}&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;";
            }
            mysqli_free_result($result);
        }
    } while(next($queries) &amp;&amp; mysqli_more_results($mysqli) &amp;&amp; mysqli_next_result($mysqli));
}
echo "&lt;/table&gt;";
if($mysqli_error=mysqli_error($mysqli)){
    echo "&lt;div&gt;Error occurred on ",key($queries);
    // echo " Query: ",current($queries),"Syntax Error:&lt;br&gt;$mysqli_error&lt;/div&gt;";  // developer-only details
    echo "&lt;/div&gt;";
}


A link to my previous work that I am leveraging for this answer.