0

I am trying to solve this. Maybe this is absolutly incorrect, I need some help.

I have a table like this:

INSERT INTO `municipios` (`id`, `provincia`, `municipio`) VALUES (1, 1, 'Alegría-Dulantzi'), (2, 1, 'Amurrio'), (3, 1, 'Añana'), (4, 1, 'Aramaio'), (5, 1, 'Armiñón'),

And this is my handler:

    $query = "SELECT * FROM municipios WHERE provincia='1'";
$result = mysql_query($query);      
while ($row = mysql_fetch_array($result)) {                 
    $groups = array(
    $row{'id'} => array(
        'name' => $row{'municipio'},
        'description' => 'Agrupación electoral de ' . $row{'municipio'},
        'status' => 'public',
        'enable_forum' => 1,
        ),
        );
}

With this I got just the last result. I have tried another options, but doesn´t work.

1
  • 2
    Use of my mysql_* is not recommended. It is deprecated and will not be supported in later versions of PHP. Commented May 6, 2013 at 18:39

2 Answers 2

2

Something like this should solve your issue:

$groups[] = array(
     $row['id'] => array(
        'name' => $row['municipio'],
        'description' => 'Agrupación electoral de ' . $row['municipio'],
       'status' => 'public',
       'enable_forum' => 1,
     ),
  );

However, may i suggest your use PDO. There is a pretty good article http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ that should get you going in the right direction.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for the answer... but I tried this before, and doesn´t work. The groups are created, but there is not any description, name... this is the result in the sql table: INSERT INTO wp_bp_groups VALUES (1, 1, '', '', '', '', 1, '2013-05-05 08:56:26'); INSERT INTO wp_bp_groups VALUES (2, 1, '', '', '', '', 1, '2013-04-25 02:32:08'); INSERT INTO wp_bp_groups VALUES (3, 1, '', '', '', '', 1, '2013-05-03 22:22:55'); I will try PDO. Thank you again.
Same trouble with PDO. Groups are created, but no data inside. This is my code: $DBH = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); $STH = $DBH->query('SELECT * FROM municipios WHERE provincia=1'); $STH->setFetchMode(PDO::FETCH_ASSOC); while($row = $STH->fetch()) { $groups[] = array( $row['id'] => array( 'name' => $row['municipio'], 'description' => 'Agrupación electoral de ' . $row['municipio'], 'status' => 'private', 'enable_forum' => 1, ), ); }
what is the output of $row ?
0

Is this an issue with the way you're appending the groups array? I think your overwriting the array every time u set a value. Try using array_push() to append to groups.

Let us know of that helps!

array_push() http://php.net/manual/en/function.array-push.php

Comments

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.