-2

I'm new to php and mysql, try to use mysql multi query function but it run many times. I couldn't find the logic need assistance.

The above mysql query are sample. Like this i have many "select sum()" statement with different condition. so I couldn't change the mysql to single query.

<html>
<title>MYsql</title>
<body>
<?php
require 'connection.php';

$query = "select count(first_name) as fName from employees;"; /* This is the sample mysql query (separate query with different condition) */
$query .= "select count(last_name) as lName from employees;"; 
$query .= "select count(title) as title from titles;"; 
$query .= "select count(first_name) as Name from employees"; 

/* Execute queries */

if (mysqli_multi_query($conn, $query)) {
do {
    /* store first result set */
    if ($result = mysqli_store_result($conn)) {
        while ($row = mysqli_fetch_array($result)) 

/* print your results */    
{ ?>
    <div class="table" style="width: 50%; float:left; padding-top: 105px">
            <table style="width: 70%"> <tr> <th colspan="2"> Lead Status </th> </tr>
            <tr>
                <td>First Name</td>
                <td><?php echo $row[0]?></td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td><?php echo $row[0]?></td>
            </tr>
            <tr>
                <td>Title</td>
                <td><?php echo $row[0]?></td>
            </tr>
            <tr>
                <td>Total Counts</td>
                <td><?php echo $row[0]?></td>
            </tr>
            </table> </div>
            <?php
}
mysqli_free_result($result);
}   
} while (mysqli_next_result($conn));
}
?>
</body>
</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

I think your query is the problem, you can optimize queries:

$query = "select count(first_name) as fName, count(last_name) as lName from employees;"; /* This is the sample mysql query (separate query with different condition) */
$query .= "select count(title) as title from titles;";  

and don't need this one,

$query .= "select count(first_name) as Name from employees"

because you have already selected first_name from first query.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Casper
  • 1,469
  • 10
  • 19
  • above queries are samples but I have many "select count" queries with different condition that's why I'm using mysqli_multi_query. – Sarath Venkat Nov 22 '17 at 06:53
  • i think it is because of queries anyway need to check them, please add them in your question. – Casper Nov 22 '17 at 07:16