PHP / MySQL - 将查询存储到多维数组中

I'm banging my head on this one.

Actually, I coded the same algorithm as this one way, way, way back. But unfortunately, I somehow forgot how. Here is a code that resemble what i need.

$result=mysql_query("select * from mydata order by 'id'")or die('died');
$num_fields = mysql_num_fields($result); 
$j=0;
$x=1;
while($row=mysql_fetch_array($result)){  
    for($j=0;$j<$num_fields;$j++){
        $name = mysql_field_name($result, $j);
        $object[$x][$name]=$row[$name];
    }$x++;
}

At the end, using this, i could access my query result something like this.

$result = array;

for($row = 1; $row < count($object); $row++){
    $result[$row]['name of my field];
}

My challenge currently is to code it in MySQLi using OOP rather than Procedural.

I do hope someone can help.

Every time you find yourself banging your head, that means you are doing some elementary task in a most monstrous and inconvenient way.

this is the code you have to had with old mysql driver.

$object = array();
$result = mysql_query("select * from mydata order by id");
while($row = mysql_fetch_assoc($result)){
    $object[] = $row;
}

and there is no problem to rewrite it using mysqli.

however, instead of coding it in MySQLi using OOP, you have to code it in PDO using OOP:

$object = $pdo->query("select * from mydata order by id")->fetchAll();

I got it working.

I feel I got to share it.

    function QueryToArray($result){
        $fields = array();
        $resultset = array();
        $x = 1;
        $y = 1;

        while ($property = mysqli_fetch_field($result)) {
            $fields[$x] = $property->name;
            $x++;
        }

        while($rows = mysqli_fetch_array($result)){
            for($x = 1; $x <= count($fields); $x++){
                $fieldname = $fields[$x];
            $resultset[$y][$fieldname] = $rows[$fieldname];
            }
            $y++;
        }               
        return $resultset;
    }

So, this is my function (I know I can still make it cleaner).

With this, all I got to code whenever I query my tables is this.

$result = $this->QueryToArray($result);

for($x = 1; $x <= count($result); $x++){
    echo $result[$x]['Name'];   
    echo " - " . $result[$x]['Description'] . "<br>";
    /* $result[row number][field name]  */  
}

I makes my life easier and I can do paging easier.

though its not purely OOP but it does the work.