从数据库中检索数据

发布于 2024-10-08 16:06:28 字数 429 浏览 0 评论 0原文

请帮我从数据库中检索数据并显示它。

我正在学习,这阻碍了我前进:(

我有:

    $result = mysql_query($sql);
    $num_reg = mysql_num_rows($result); 
    echo $num_reg; // this shows 3 

    while($row = mysql_fetch_assoc($result))
      {  

        foreach($row as  $value)
       {  


    //Now I need to operate with the rows values.

echo $row['PHONE'];

但这不是打印电话号码,而是每个打印 5 次

我做错了什么?

非常感谢

Please give me a hand retrieving data from the database and showing it.

I'm learning and this is holding me from advancing :(

I have:

    $result = mysql_query($sql);
    $num_reg = mysql_num_rows($result); 
    echo $num_reg; // this shows 3 

    while($row = mysql_fetch_assoc($result))
      {  

        foreach($row as  $value)
       {  


    //Now I need to operate with the rows values.

echo $row['PHONE'];

But this instead of printing the phone numbers, prints them 5 times each

What am I doing wrong?

Thanks a lot

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

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

发布评论

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

评论(4

云淡月浅 2024-10-15 16:06:28

删除 foreach 循环

while($row = mysql_fetch_assoc($result))
{  
 echo $row['PHONE'];
 echo $row['NAME'];
 echo $row['OTHER_FIELD'];
}

Remove foreach loop

while($row = mysql_fetch_assoc($result))
{  
 echo $row['PHONE'];
 echo $row['NAME'];
 echo $row['OTHER_FIELD'];
}
奢欲 2024-10-15 16:06:28

在这种情况下你不需要 foreach

while($row = mysql_fetch_assoc($result))
{  
    echo $row['PHONE'];
}

可能你的 $row 有 2 个元素,并且在 foreach 中你的 echo $row['PHONE']每个元素被调用一次

you don't need foreach in that case

while($row = mysql_fetch_assoc($result))
{  
    echo $row['PHONE'];
}

Probably your $row has 2 elements, and inside foreach your echo $row['PHONE'] is called once for every element

牵你手 2024-10-15 16:06:28

在 while 循环中执行 print_r($row) ,在 foreach 循环中执行 print_r($value) 。
然后问你为什么要在 $row 元素上循环 echo $row 。

do a print_r($row) in your while loop and a print_r($value) in your foreach loop.
Then ask you why do you echo $row in a loop on $row elements.

¢蛋碎的人ぎ生 2024-10-15 16:06:28

您不需要在 while() 循环内使用第二个 foreach() 循环来处理这些值。您在这里所做的是循环遍历行,然后循环遍历值,但如果您已经可以通过 $row 变量访问这些值,则不需要再次循环。您收到电话号码 5 次的事实表明您的表格中有 5 列。

一个例子 - 删除 foreach() 循环:

while($row = mysql_fetch_assoc($result)){
    echo $row['PHONE'];
    echo $row['NAME'];
    echo "<br />";
}

我猜你有一个名为“name”的变量,但如果不是,请将其交换为你已有的变量。最后一个 echo 只是打印一个新行以使其更易于阅读。

You don't need a second foreach() loop inside the while() loop to work on the values. What you're doing here is looping through the rows then looping through the values but if you already have access to the values via the $row variable you don't need to loop again. The fact you get the phone number 5 times suggests you have 5 columns in your table.

An example - remove the foreach() loop:

while($row = mysql_fetch_assoc($result)){
    echo $row['PHONE'];
    echo $row['NAME'];
    echo "<br />";
}

I'm guessing you have a variable called "name" but if not just swap it for one you do have. The last echo just prints a new line to make it easier to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文