-1

I am a beginner with php and PDO and need help solving this error that I keep getting on this line of code $category_name [$category["category_id"]] = $category["category"]; I continue to get, Notice: Undefined index: category. Also Undefined index: category_id. I dont have a clue as to why, could someone provide me with the correct way to do this. Here is my code. This one slightly different because it used in a while statement.

 // get the item category names

    $category_name = Array();
    $query = 'SELECT * FROM category_2';
    $categories_list = $db->prepare($query);
    $categories_list->execute();
    while ($category = $categories_list->fetchAll())

{

        $category_name [$category["category_id"]] = $category["category"];

    }
user3135712
  • 57
  • 1
  • 7

2 Answers2

1

You're probably looking for fetch(PDO:: FETCH_ASSOC) instead of fetchAll()

NewInTheBusiness
  • 1,465
  • 1
  • 9
  • 14
  • 1
    That's what I said in my comment :P – Mike Jan 09 '14 at 04:16
  • Typing from a phone. Cant even mark it up correctly. Well didnt see that plus you're suggesting the default which is returning both indexed by column names and numbers when it seems he only needs an associative array returned. Almost same though but not quite :) – NewInTheBusiness Jan 09 '14 at 04:22
  • @NewInTheBusiness Not necessarily. The default fetch mode can be set at the statement and even connection level prior to calling `fetch()` – Phil Jan 09 '14 at 04:28
  • He claims to be a beginner... – NewInTheBusiness Jan 09 '14 at 04:31
1

try this: We will have single array in each time in loop in $category. You can access any database attributes in this array like this: $category['attribute_name']

while ($category = $categories_list->fetch(PDO::FETCH_ASSOC))

{

   $category_name [$category["category_id"]] = $category["category"];

}
print_r($category_name);
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53