简体   繁体   English

SQL结果作为PHP数组

[英]SQL Results as PHP Array

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. 我尝试过另一种选择,但不起作用。

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. 但是,我可以建议您使用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. 有一篇非常好的文章http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/应该让你朝着正确的方向前进。

Is this an issue with the way you're appending the groups array? 这是你附加groups数组的方式的问题吗? I think your overwriting the array every time u set a value. 我想你每次设置一个值都会覆盖数组。 Try using array_push() to append to groups. 尝试使用array_push()附加到组。

Let us know of that helps! 让我们知道这有帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM