View Single Post
Old 09-04-2006, 11:51 AM   #5 (permalink)
pr0gr4mm3r
Junior Member
 
Join Date: Sep 2006
Location: Behind You
Posts: 12
Default Re: Display mysql table fields and data

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 "&nbsp;-&nbsp;" 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
__________________
Life would be easier if I had the source code.
pr0gr4mm3r is offline   Reply With Quote
Sponsored Links