Getting an associative array resultset from mysqli

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

I am running the following code to attempt and extract all of the data from the database using MySQLi.
PHP Code: [ Select ]
// We do an associative array
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
    $rows[] = $row;
}
 
// Returning the array
return $rows;
  1. // We do an associative array
  2. while($row = $result->fetch_array(MYSQLI_ASSOC))
  3. {
  4.     $rows[] = $row;
  5. }
  6.  
  7. // Returning the array
  8. return $rows;

That gives me:
Code: [ Select ]
Array
(
  [0] => Array
    (
      [0] => Post
      [1] => 1
    )

  [1] => Array
    (
      [0] => Message
      [1] => 1
    )

)
  1. Array
  2. (
  3.   [0] => Array
  4.     (
  5.       [0] => Post
  6.       [1] => 1
  7.     )
  8.   [1] => Array
  9.     (
  10.       [0] => Message
  11.       [1] => 1
  12.     )
  13. )

But it's supposed to give me
Code: [ Select ]
Array
(
  [0] => Array
    (
      [Permission_Name] => Post
      [Permission_Value] => 1
    )

  [1] => Array
    (
      [Permission_Name] => Message
      [Permission_Value] => 1
    )

)
  1. Array
  2. (
  3.   [0] => Array
  4.     (
  5.       [Permission_Name] => Post
  6.       [Permission_Value] => 1
  7.     )
  8.   [1] => Array
  9.     (
  10.       [Permission_Name] => Message
  11.       [Permission_Value] => 1
  12.     )
  13. )

I've tried the following method
PHP Code: [ Select ]
// We do an associative array
foreach($result->fetch_array(MYSQLI_ASSOC) as $key => $value)
{
    $rows[$key] = $value;
}
 
// Returning the array
return $rows;
  1. // We do an associative array
  2. foreach($result->fetch_array(MYSQLI_ASSOC) as $key => $value)
  3. {
  4.     $rows[$key] = $value;
  5. }
  6.  
  7. // Returning the array
  8. return $rows;

But that returns
Code: [ Select ]
Array
(
  [0] => Post
  [1] => 1
)
  1. Array
  2. (
  3.   [0] => Post
  4.   [1] => 1
  5. )

Only one result, even though I have two... and still numeric and not associative. I can still work with numeric, but it still bothers me that I can't get it to work... I don't want to compromise, I want to make it work.

According to php.net comments I'm doing it correctly, but it doesn't seem to work for me for some reason. (First comment... second one (though it might work) is big and ugly code... I'm trying to keep mine as simple, yet effective, as possible).

Thanks for any help.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

I just tried the following method
PHP Code: [ Select ]
// We do an associative array
while(list($key, $value) = $result->fetch_array(MYSQLI_ASSOC))
{
    $rows[$key] = $value;
}
 
// Returning the array
return $rows;
  1. // We do an associative array
  2. while(list($key, $value) = $result->fetch_array(MYSQLI_ASSOC))
  3. {
  4.     $rows[$key] = $value;
  5. }
  6.  
  7. // Returning the array
  8. return $rows;

Which gave me the following result
Code: [ Select ]
Array
(
  [Post] => 1
  [Message] => 1
)
  1. Array
  2. (
  3.   [Post] => 1
  4.   [Message] => 1
  5. )

Pretty interesting and seems to be even better then the previous method, but I don't think that this is what it means by associative array from mysqli... at least mysql_fetch_assoc() doesn't work this way.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Okay, nevermind, I solved my error :lol: quite a terrible mistake to do and be stumped about :lol: I seem to do quite a lot of these...

I was doing the following:
PHP Code: [ Select ]
if($type = 'assoc')
{
    // We do both, numeric and associative array
    while($row = $result->fetch_array(MYSQLI_ASSOC))
    {
        $rows[] = $row;
    }
   
    // Returning the array
    return $rows;
}
  1. if($type = 'assoc')
  2. {
  3.     // We do both, numeric and associative array
  4.     while($row = $result->fetch_array(MYSQLI_ASSOC))
  5.     {
  6.         $rows[] = $row;
  7.     }
  8.    
  9.     // Returning the array
  10.     return $rows;
  11. }

PHP Code: [ Select ]
if($type = 'assoc')

Instead of
PHP Code: [ Select ]
if($type == 'assoc')

For some reason that seemed to throw it off.
Now I get
Code: [ Select ]
Array
(
  [0] => Array
    (
      [permission_name] => Post
      [permission_value] => 1
    )

  [1] => Array
    (
      [permission_name] => Message
      [permission_value] => 1
    )

)
  1. Array
  2. (
  3.   [0] => Array
  4.     (
  5.       [permission_name] => Post
  6.       [permission_value] => 1
  7.     )
  8.   [1] => Array
  9.     (
  10.       [permission_name] => Message
  11.       [permission_value] => 1
  12.     )
  13. )

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 60 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.