从变量中的mysql行获取所有数据 [英] Get all data from mysql row in a variable

查看:34
本文介绍了从变量中的mysql行获取所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被一些小事困住了,希望你能帮助我..

I'm stuck on something small, hope you can help me..

我需要将所有电话号码(即表 'leerlingen' 中的一列)放在一起,用逗号分隔,因此我可以将其插入到另一个脚本中,它会显示所有电话号码,如下所示:

I need all phone numbers (thats a column in table 'leerlingen') in one here, with comma's separated, so I can insert that here in another script and it shows all phone numbers, like this:

0612345637,061231823,061231723  

代码:

$result = mysql_query("SELECT * FROM leerlingen
WHERE klas_id='$aan_klas'");

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

    $variabele = $row['leerling_nummer'] . ",";

  echo "$variabele";
  }

但是,当我 echo $variabele OUTSIDE while 循环时,它只给我一个电话号码.当我 echo $variabele INSIDE while 循环时,它会以正确的方式向我显示所有电话号码.. 有什么想法吗?

But, when I echo $variabele OUTSIDE the while loop, it only gives me ONE phone number. When I echo $variabele INSIDE the while loop, it shows me all the phone numbers, on the right way.. Any ideas?

编辑

我看到我的代码根本不起作用,所以这就是我想要的:它应该用逗号分隔所有电话号码,放入变量或数组中.

my code isn't working at all I see, so here's what I want: It should get all phone numbers seperated with a comma, put in a variabele or array.

编辑

谢谢大家,这是工作标志!但是...只有当 al mysql_ 事物是 mysql_ 时.当我将它们更改为 mysqli_ 时,没有任何反应...

Thanks guys, it's workign! But... only when al mysql_ things ARE mysql_. When I change them to mysqli_, nothing happens...

推荐答案

当您 echo "$variabele"; 时看到所有电话号码的原因>inside while 循环,是因为在每次迭代中(每次重新进入 while 循环,可以这么说)您都在回显 $variabele 的最后一个值.因此,它看起来好像所有的东西都是一次echo,而实际上它是一次echo.

The reason why you are seeing all phone numbers when you echo "$variabele"; inside the while loop, is because on each iteration (every time the while loop is re-entered, so to speak) you are echoing the last value of $variabele. And so, it looks as though everything was echo'd all at once, when in fact it is echo'd one at a time.

您可以通过将代码更改为以下内容来轻松演示这一点:

You can easily demonstrate this by altering the code to:

while($row = mysql_fetch_array($result))
{
    $variabele = $row['leerling_nummer'] . ",";
    echo "$variabele" . " | ";
}

现在,您将看到结果不是(正如我认为的那样):

Now, you will see the result is not (as I think you expected):

0612345637,061231823,061231723,|

...但是:

0612345637, |061231823, |061231723, |

如果您认为正在发生的事情真的发生了,那么结果实际上是这样的:

If what you believed was happening, was really happening, then the result would actually be this:

0612345637, |0612345637, 061231823, |0612345637、061231823、061231723、|

如果您想在一个变量中收集所有电话号码,最好使用数组,如 Mark Ba​​ker 已经提议您必须将每个新电话号码附加到$variabele 字符串,如下所示:

If you want to collect all phone numbers in one variable, you'd indeed be best off by using an array, as Mark Baker already proposed, or you would have to append each new phone number to the $variabele string, like so:

// initiate $variabele as an empty string
$variabele = "";
while($row = mysql_fetch_array($result))
{
    // append the new phone number to $variabele (note .= in stead of =)
    $variabele .= $row['leerling_nummer'] . ",";
}
echo $variabele;

唯一的问题是你必须删除最后一个逗号.有多种方法可以做到这一点或规避这一点,但 Mark Ba​​ker 的解决方案实际上要简单得多.

The only problem with this, is that you'd have to remove the last comma then. There are a variety of ways to do this, or circumvent this, but Mark Baker's solution is much simpler actually.

最后,我想让你知道一个漂亮的 MySQL 函数,它允许你轻松地连接列的值;这是函数 GROUP_CONCAT().但是,这在您当前的情况下可能没有用,因为您也在获取其他列.但是为了将来参考,如果您只需要一列想要用逗号连接它们,您可以这样做:

Lastly, I want to make you aware of a nifty MySQL function that allows you to easily concatenate values of a column; this is the function GROUP_CONCAT(). This, however, is probably not useful in your current case, since you are fetching other columns as well. But for future reference, if you only need one column want to concatenate them with a comma, you could do:

SELECT GROUP_CONCAT( leerling_nummer ) AS leerling_nummers
FROM leerlingen
WHERE klas_id='$aan_klas'
GROUP BY klas_id

在查询数据库然后执行 $row = mysql_fetch_array($result); 之后,所有数字都将在 $row['leerling_nummers'] 中,由一个逗号,因为 GROUP_CONCAT() 使用逗号作为默认分隔符.换句话说:您不再需要使用 PHP 遍历行;MySQL 已经为您收集了所有内容.

After querying the database and then doing $row = mysql_fetch_array($result); all numbers would then be in $row['leerling_nummers'], seperated by a comma, because GROUP_CONCAT() uses a comma as a default separator. In other words: you wouldn't need to loop over rows with PHP anymore; MySQL has already collected everything in one row for you.

这篇关于从变量中的mysql行获取所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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