Database to php array storge?

Hi there,

I have this data stored in arrays like below and I decided I wanted to store it in the
database instead.

Is my table a correct representation of the arrays, and if so, how do I pull the
data back out of the database, and still have it represent the way it was stored in the
arrays thanks :slight_smile:


id| parent_id | date | user | text

1 | 0 | 2012/01/20 | Leon | Some text one
2 | 1 | 2012/01/20 | Brad | Some text two
3 | 0 | 2012/01/20 | Chris | Some text three

[php]$data = array
(
1 => array
(
“parent_id” => 0, “date” => “2012/01/20”, “user” => “Leon”, “text” => “Some text one”
),
2 => array
(
“parent_id” => 1, “date” => “2012/01/20”, “user” => “Brad”, “text” => “Some text two”
),
3 => array
(
“parent_id” => 0, “date” => “2012/01/20”, “user” => “Chris”, “text” => “Some text three”
)
);[/php]

Yes that should work

Hello, thanks for your reply!

So how would one put the data back into an array from the database?
I know this is not correct below I am just putting there for reference :slight_smile:

[php]$q = mysql_query(“SELECT * FROM table”);

$data = array();

while($row = mysql_fetch_assoc($q))
{
$data [$row[‘parent’]][] = $row;
}[/php]

Obviously this is quite simle and you would have something like…

[php]$q = mysql_query(“SELECT * FROM table”);$data = array();

$data = array();

for($i = 0; $i < mysql_num_rows($q); $i++)
{
$data [$i] = mysql_fetch_assoc($q);
}[/php]

For some reason the code I was testing on needs the array index to start at one.
Which threw me off O_o

Sponsor our Newsletter | Privacy Policy | Terms of Service