选择MySQL表数据到一个数组 [英] Selecting MySql table data into an array

查看:248
本文介绍了选择MySQL表数据到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从MySQL赶数据,把他们所有的阵列。
假设:

  users表
    -----------------------
    ID |名称| code
    ----------------------
    1 |峡谷| 2132
    2 | FLIX | ksd02
    3 |者jasmen | skaod2    $ SQL =的mysql_query(选择编号,名称,$ C $从用户C);
    $用户信息=阵列()
    而($ row_user = mysql_fetch_array($ SQL)){
    $用户信息= $ row_user [名]
    }
-------------------------
的foreach($用户信息作为美国参考$){
回声$美国参考< BR />中。
}

下面是我只能插入用户名,但不能同时插入code和问题;用户信息数组中的ID,请帮我在插入同一阵列中的所有数据。

[P.S]没有面向对象吧。


解决方案

  $ SQL =的mysql_query(从用户选择的ID,姓名,code);
$用户信息=阵列();而($ row_user = mysql_fetch_assoc($ SQL))
    $用户信息[] = $ row_user;

这会给你 $用户信息为具有以下结构的数组:

  [
    [ID => 1,名称= GT; '峡谷',code => '2123'],
    [ID => 2,名称=> 毛皮,code => 'ksd02'],
    [ID => 3,名称=> '者jasmen',code => skaod2']
]

如果您要输出的数据:

 的foreach($用户信息为$ USER){
    回声ID:{$用户[ID]}< BR />中
       。 姓名:{$用户[名]}< BR />中
       。 code:{$用户[code]}< BR />< BR />中;
}

I try to catch data from mysql to put them all in array. Suppose:

    users table
    -----------------------
    id| name | code 
    ----------------------
    1| gorge | 2132
    2| flix | ksd02
    3| jasmen | skaod2

    $sql = mysql_query("select id, name, code from users");
    $userinfo = array()
    while($row_user = mysql_fetch_array($sql)){
    $userinfo = $row_user[name] 
    }
-------------------------
foreach($userinfo as $usinfo){
echo $usinfo."<br/>";
}

Here is the problem i can only insert user name but cant insert also code & id in userinfo array please help me to insert all data in same array.

[P.S] No object oriented please.

解决方案

$sql = mysql_query("select id, name, code from users");
$userinfo = array();

while ($row_user = mysql_fetch_assoc($sql))
    $userinfo[] = $row_user;

This will give you $userinfo as an array with the following structure:

[
    [id => 1, name => 'gorge', code => '2123'],
    [id => 2, name => 'flix', code => 'ksd02'],
    [id => 3, name => 'jasmen', code => 'skaod2']
]

If you want to output the data:

foreach ($userinfo as $user) {
    echo "Id: {$user[id]}<br />"
       . "Name: {$user[name]}<br />"
       . "Code: {$user[code]}<br /><br />";
}

这篇关于选择MySQL表数据到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆