简体   繁体   中英

How to put SQL query in a for() loop

Look at this code first:

<?php

$dbhost = 'xxxxx';
$dbuser = 'xxxxxx';
$dbpass = 'xxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  //Some code
}

$sql1 = 'SELECT AVG(time) FROM lucky WHERE interviewer=1';
$sql2 = 'SELECT AVG(time) FROM lucky WHERE interviewer=2';
.
.
$sql100 = 'SELECT AVG(time) FROM lucky WHERE interviewer=100';

mysql_select_db('xxxxxx');

$retval1 = mysql_query( $sql1, $conn );
$avg1 = mysql_result($retval1,0);
.
.
$retval100 = mysql_query( $sql100, $conn );
$avg100 = mysql_result($retval100,0);

if (! $retval)
{
  //Some code
}

mysql_close($conn);

?>

<table width='1100' border='0' align='center'>
  <tr>
    <td>1</td>
    <td><?php echo round($avg1, 2); ?></td>
  </tr>
  .
  .
  <tr>
    <td>100</td>
    <td><?php echo round($avg100, 2); ?></td>
  </tr>
</table>

What actually happening is if interviewer 1 is found in db, then its average of time is to be calculated and then displayed in the table below. It has to be done for 100 interviewers. All I want is to do this using a shorter method like using a for() loop. Additionally, I want that if interviewer 1 is not found in db, I don't want to display the blank row. I want to display it only if there is a hit. Please help.

You REALLY don't want to put SQL in a loop You'll be executing 100 queries instead of a single one.

That will kill your website's performance, when you can just use simple aggregation as illustrated below for optimal performance.

Also, the mysql library is deprecated, use mysqli instead.

Do this:

$sql = 'SELECT interviewer, AVG(time) AS avg_time 
        FROM lucky 
        WHERE interviewer BETWEEN 1 AND 100
        GROUP BY interviewer';

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

// Execute the query
$result = $mysqli->query($sql);

// Display the results in a table
echo '<table><tbody>';
while ($row = $result->fetch_assoc()) {
    echo '<tr>';
    echo '<td>' . $row['interviewer'] . '</td>';
    echo '<td>' . round($row['avg_time'], 2) . '</td>';
    echo '</tr>';
}
echo '</tbody></table>';

// Close the resultset
$result->close();

What I'm doing above is pulling all interviewers' average times, grouping them together by interviewer #.

This will automatically eliminate missing interviewers at the database level.

Then, I'm looping through the DB results with a while loop.

This is the accepted programming practice for querying and displaying multiple items from your database. The DB is very fast at filtering and aggregating data. Use it for what it's good at. If you try to pull that logic out into PHP, and query the database 100 times, you'll add 100x the latency and connection handling for nothing.

try this code

for($i=1; i<=100; $i++)
{
$sql1 = 'SELECT AVG(time) FROM lucky WHERE interviewer='.$i;

}

OR

$sql1 = 'SELECT AVG(time) FROM lucky WHERE interviewer BETWEEN 1 AND 100

echo "<table width='1100' border='0' align='center'>";
  for($i=1; i<=100; $i++)
  {
   $sql1 = 'SELECT AVG(time) FROM lucky WHERE interviewer='.$i;
    echo "<tr>";

     mysql_select_db('xxxxxx');
     $retval1 = mysql_query( $sql1, $conn );
     $avg1 = mysql_result($retval1,0);
     echo "</tr>";
   }
 echo "</table>"; 

YOU may use this simple query.

SELECT avg(time) FROM lucky GROUP BY  interviewer

No need to looping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM