SilentUK
SilentUK

Reputation: 195

populate php array with while loop?

Is there a way to output an sqlite table to a php array?

I am currently trying to use a while loop but it does not display correctly.

$db = sqlite_open ("products.db", 0666, $error);
$result=sqlite_query($db,"SELECT * from Books");
$products = array();
while($row=sqlite_fetch_array($result,SQLITE_ASSOC))
{
    $products = $row;
}

I want this to store into a 2D php array as if it were:

$products = array(
    1 => array(
        'name' => '',
        'price' => ,
        'category' => '',
        'description' => ''
    ),
    2 => array(
        'name' => '',
        'price' => ,
        'category' => '',
        'description' => ''
    ),
    3 => array(
        'name' => '',
        'price' => ,
        'category' => '',
        'description' => ''
    )
);

Upvotes: 0

Views: 1124

Answers (1)

John Conde
John Conde

Reputation: 219804

You're close. You just need to add each row to your array instead of overwriting the array variable which you are currently doing..

while($row=sqlite_fetch_array($result,SQLITE_ASSOC))
{
    $products[] = $row;
}

Upvotes: 4

Related Questions