This function is a simple query displayer. The only thing you have to do is simply create a variable with a valid SELECT statement as mentioned @ the examples…. echo the function and you will see your results! CSS is not included!
Simple query displayer
function db_query($query_string)
{
// Query result
$result = mysql_query($query_string) or die ('Check your query!');
// Total fields
$amount_fields = mysql_num_fields($result);
// Start table
$table = '';
$table .= '<table>';
// Create the header cells
$table .= '<tr>';
for ($i = 0; $i < $amount_fields; $i++)
{
$name_fields = mysql_field_name($result,$i);
$table .= '<th>'.$name_fields.'</th>';
}
$table .= '</tr>';
// Create all the rows including the records from the recordset
while ($r = mysql_fetch_row($result))
{
$table .= '<tr>';
for ($j = 0; $j < $amount_fields; $j++)
{
$table .= '<td>'.$r[$j].'</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
// Return the table as string
return $table;
}
Example of use
$query = "SELECT * FROM movies"; echo db_query($query);
$query = "SELECT title, writter FROM books"; echo db_query($query);
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)


» Latest comments