Simple query displayer

Views: 4 Last modified: August 03rd, 2011 Comments: 0

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);
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
    Bluehost

    Mail this!

    To: From:Sum {0+5} =  
    Anything to add ?

        You must be logged in to post a comment.