0

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' => ''
    )
);
1
  • use an array assignment, every iteration, its overwritten, you could also use your ID as the key in assignment $products[$row['id']] etc. Commented Mar 17, 2015 at 0:24

1 Answer 1

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this has fixed the issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.