选择MySql表数据放入数组中

发布于 2024-10-12 19:45:23 字数 567 浏览 3 评论 0原文

我尝试从 mysql 捕获数据并将它们全部放入数组中。 假设:

    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/>";
}

这是我只能插入用户名但无法插入代码和的问题。 userinfo 数组中的 id 请帮助我将所有数据插入同一数组中。

[PS] 请不要面向对象。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我闹别瞎闹 2024-10-19 19:45:23
$sql = mysql_query("select id, name, code from users");
$userinfo = array();

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

这将为您提供 $userinfo 作为具有以下结构的数组:

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

如果您想输出数据:

foreach ($userinfo as $user) {
    echo "Id: {$user[id]}<br />"
       . "Name: {$user[name]}<br />"
       . "Code: {$user[code]}<br /><br />";
}
$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 />";
}
半透明的墙 2024-10-19 19:45:23
while($row_user = mysql_fetch_assoc($sql)){
   $userinfo[] = $row_user;
}

foreach($userinfo as $usrinfo){
   echo "Id: ".$usrinfo['id']."<br />";
   echo "Name: ".$usrinfo['name']."<br />";
   echo "Code: ".$usrinfo['code']."<br />";
}
while($row_user = mysql_fetch_assoc($sql)){
   $userinfo[] = $row_user;
}

foreach($userinfo as $usrinfo){
   echo "Id: ".$usrinfo['id']."<br />";
   echo "Name: ".$usrinfo['name']."<br />";
   echo "Code: ".$usrinfo['code']."<br />";
}
柳若烟 2024-10-19 19:45:23

我找到了这段代码,我节省了时间:

<?php
$sql=mysql_query("select * from table1");

/*every time it fetches the row, adds it to array...*/
while($r[]=mysql_fetch_array($sql));

echo "<pre>";
// Prints $r as array 
print_r ($r);
echo "</pre>";
?>

I found this code and I save my day:

<?php
$sql=mysql_query("select * from table1");

/*every time it fetches the row, adds it to array...*/
while($r[]=mysql_fetch_array($sql));

echo "<pre>";
// Prints $r as array 
print_r ($r);
echo "</pre>";
?>
千紇 2024-10-19 19:45:23
   while($row_user = mysql_fetch_array($sql)){
    $row_id = $row_user ["id"];
        $row_author = $row_user ["author"];
        $row_code = $row_user ["code"];
       $userinfo[] = array("id"=> $row_id , "author"=> $row_author, "code"=> $row_code);
    }
foreach ($userinfo as $user) {
    echo "Id: {$user[id]}\n"
       . "Name: {$user[name]}\n"
       . "Code: {$user[code]}\n\n";
}
   while($row_user = mysql_fetch_array($sql)){
    $row_id = $row_user ["id"];
        $row_author = $row_user ["author"];
        $row_code = $row_user ["code"];
       $userinfo[] = array("id"=> $row_id , "author"=> $row_author, "code"=> $row_code);
    }
foreach ($userinfo as $user) {
    echo "Id: {$user[id]}\n"
       . "Name: {$user[name]}\n"
       . "Code: {$user[code]}\n\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文