Array help

Hi,

im using this function as the method of retrieving DB results via SELECT statements and putting them into an array.

function select($sqlquery) {

    $query = mysql_query($sqlquery);

    $result = array();
    $table = array();
    $field = array();
    $tempResults = array();
    $numOfFields = mysql_num_fields($query);

    for ($i = 0; $i < $numOfFields; ++$i) {
        array_push($table,mysql_field_table($query, $i));
        array_push($field,mysql_field_name($query, $i));
    }

    while ($row = mysql_fetch_row($query)) {
            for ($i = 0;$i < $numOfFields; ++$i) {
                //$table[$i] = trim(ucfirst($table[$i]),"s");
                $table[$i] = ucfirst($table[$i]);
                $tempResults[$table[$i]][$field[$i]] = $row[$i];
            }

    array_push($result,$tempResults);
    }

    return $result;

}

the problem ive got is when doing a UNION select statement the array is returned in the following format:

Array ( [0] => Array ( => Array ( [username] => test [password] => qwerty [userid] => 19 ) ) )

the problem lies with the additional array key which is blank.

how can i remove this so this so it resemble normal select statement arrays like this:

Array( [0] => Array ( [username] => james [password] => qwerty [userid] => 19 [chatcomment] => gggggggggfghgfh [usertype] => Tblstudents ) ) )

or even better, does anyone have any suggestions about how i can do this better and make sure the array is always the correct way.

thanks,

phphelpee

Is there any reason you are not using the native mysql functions?
mysql_fetch_assoc - returns an associative array of the row results
mysql_fetch_array - returns a numeric array of results

references
PHP: mysql_fetch_assoc - Manual
PHP: mysql_fetch_array - Manual

The fetching of the results can be done like this:

<?php

// query run here

$results_array=array();
while ( $row = mysql_fetch_array($result, MYSQL_BOTH) ) {
    $results_array[] = $row;
}

return $results_array;

?>

Thanks for that it worked!,

ive modded it slightly so it the tablename and fieldname are the array element names.

code below;



function select($sqlquery) {


        $query = strtolower($sqlquery);
        $result = mysql_query($query);

        if (!($row = mysql_fetch_array($result)))
        {
         return null;
        }

        $assoc = Array();
        $rowCount = mysql_num_fields($result);

        for ($i = 0; $i < $rowCount; $i++)
        {
             $table = mysql_field_table($result, $i);
             $field = mysql_field_name($result, $i);
             $assoc["$table.$field"] = $row[$i];

         }

    return $assoc;

but now im stuck with the following problem
the above works fine for SELECT statements which are formatted as such tbltable.fieldname, however for COUNT(tblname.fieldname) the returned array name is “.count”. How can i, before it gets renamed, remove “COUNT()” and leave only the correct format of tblname.fieldname then add on the end the word “COUNT”?

preg_replace doesnt seem to do what i want.

thanks,