Hi Shaun

On 01 Apr,2003 at 14:28 shaun shaun wrote:

> thanks for your reply,
> What i was after is a way of representing the data, given that you dont know
> what columns will be displayed and how many rows etc?

One way to do it is to load the data into arrays.  I generally use mysql_fetch_array() 
to access recordsets.  This delivers both an Indexed and associative array of values.  
You can modify this behaviour with the last argument.  Assuming you want to preserve 
the keys (which will be the column headings in the sql result), in mysql_fetch_array() 
you would use the constant MYSQL_ASSOC.  You could do something like this:-


// Assume you have a db connection

if (($result = mysql_query($sql_from_textarea))===0) {
        // Drop out gracefully

// Check we have some rows
if (mysql_num_rows($result)) {
        // We have some rows so load up an array with your recordset
        // Start a counter
        // Step thru the recordset
        while($data = mysql_fetch_array($result,MYSQL_ASSOC)) {
                // loop through the current row, listing keys and values
                while (list($k,$v)=each($data)) {
                        // create a new array member of the form
                        // $myarray[index][key]=value

                // increment the index counter
        // Now we have an array which looks like the data so we can     // display it 
(you could do the keys bit up above as well
        // actually)

        // get the keys from the first element - safe cos we know 
        // we have at least one row
        $keys = array_keys($myarray[0]);

        // Now a standard loop through to first display the keys
        // as headings then the data

        $html = "<table><tr>";

        // Create a row of headings
        for ($i=0;$i<count($keys);$i++) {
                $html .= "<td><b>".$keys[$i]."</b></td>";

        // Now loop through the rest of the data
        while (list(,$v)=each($myarray)) {
                $html .= "<tr>";
                // Send keys array pointer back to the start
                while(list(,$b)=each($keys)) {
                        $html.= "<td>".$v[$b]."</td>";
                $html .= "</tr>";
        $html .= "</table>";

        echo $html;
} else {
        // Drop out gracefully


Does that all make sense ?  Basically, you grab the recordset into an array whose 
dimensions you can loop through.  Then you grab the keys from an array element to give 
you the columns.  Then you loop through, using the keys as your guide for when to 
change to a new row.

There may well be a more elegant way to do it but this would be where I would start !!

> I would only be looking to do 'selects' so how can i ensure that this is the
> only type of query run?

Make sure the user you run the script as, only has select permissions on the db you 
are using.

Once again, hope this helps ;-)

t: 01903 739 997
w: www.thelittledot.com

The Little Dot is a partnership of
Ronan Chilvers and Giles Webberley

PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to