I have some questions , please help me.
- In PHP/MySQL I have a query that I want to generate 2D array, how can I do it ?
I hava a php array code, like this:
$menu = Array( // I wanna to get data from a SQL SELECT. Array('id'=>1,'title'=>'Menu 1', 'parent_id'=>null), Array('id'=>2,'title'=>'Sub 1.1', 'parent_id'=>1), Array('id'=>3,'title'=>'Sub 1.2', 'parent_id'=>1), Array('id'=>4,'title'=>'Sub 1.3', 'parent_id'=>1), Array('id'=>5,'title'=>'Menu 2', 'parent_id'=>null), Array('id'=>6,'title'=>'Sub 2.1', 'parent_id'=>5), Array('id'=>7,'title'=>'Sub Sub 2.1.1', 'parent_id'=>10), Array('id'=>8,'title'=>'Sub 2.2', 'parent_id'=>5), Array('id'=>9,'title'=>'Menu 3', 'parent_id'=>null), Array('id'=>10,'title'=>'Menu 3', 'parent_id'=>null), );
How could I to generate this simple array using mysql ?
<?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT * FROM mytable"); //this is wrong,but I wanna to reach this effect↓↓↓↓↓↓↓↓ while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "Array('id'=>$row["id"],'title'=>'$row["title"]','parent_id'=>$row["parent_id"])," } ?>