I have this function in my main include file to debug SQL queries:
PHP Code:
function DumpSQL($result)
{
if($result == 0)
{
echo "<b>Error ".mysql_errno().": ".mysql_error()."</b>";
}
elseif (@mysql_num_rows($result) == 0)
{
echo("<b>Query completed. No results returned.</b><br>");
}
else
{
echo "<table border='1'>
<thead>
<tr><th>[Num]</th>";
for($i = 0;$i < mysql_num_fields($result);$i++)
{
echo "<th>" . $i . " - " . mysql_field_name($result, $i) . "</th>";
}
echo " </tr>
</thead>
<tbody>";
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
echo "<tr><td>[$i]</td>";
$row = mysql_fetch_row($result);
for($j = 0;$j < mysql_num_fields($result);$j++)
{
echo("<td>" . $row[$j] . "</td>");
}
echo "</tr>";
}
echo "</tbody>
</table>";
} //end else
}
Then to use it, do it this way:
PHP Code:
$query = "SELECT * FROM Table";
$result = mysql_query($query);
DumpSQL($result);
Query to limit your results (The limit clause goes after the WHERE, ORDER BY clause if exists):
Code:
SELECT * FROM table LIMIT 20
Query that sorts:
Code:
SELECT * FROM table ORDER BY field