# Calculating SQL query processing

All I want do to is simply get the result of how long (in seconds) the PHP takes to process the SQL query. Am I on a right track? Is my code correct to get the result I want?

<?php
$con = mysql_connect("localhost","root",""); if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test3", $con);$start = microtime(TRUE);
$result = mysql_query("SELECT * FROM test ORDER BY test.test2 DESC LIMIT 0,100"); echo "<table border='1'> <tr> <th>ID</th> <th>test1</th> <th>test2</th> <th>test3</th> <th>test4</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" .$row['ID'] . "</td>";
echo "<td>" . $row['test1'] . "</td>"; echo "<td>" .$row['test2'] . "</td>";
echo "<td>" . $row['test3'] . "</td>"; echo "<td>" .$row['test4'] . "</td>";
echo "</tr>";
}
echo "</table>";

$end = microtime(TRUE);$sqlTime = $end -$start;

mysql_close($con); echo$sqlTime
?>


1. Yes, microtime is designed to be used for this. The PHP documentation mentions this use case for microtime().
• Thanks, for the connection advice. I will change that, but I am still concerned whether \$end = microtime (TRUE) is the right place to have? Or should I put it after the sql query? – fn24 Mar 13 '12 at 15:44