我在mysql中有以下表格。我需要从该表中选择Hid(s)并将结果附加到字符串'$ s'。如果你可以帮忙的话会很棒。
表名:CASES
Did Hid Year Case
--- --- ---- ----
1 1 2011 6
1 1 2012 7
2 2 2011 40
2 2 2012 10
$did=1;
$yr=2011;
$s='';
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";
$r=mysql_query($q);
while($rw=mysql_fetch_assoc($r))
{
//I need to append the Hid(s) to a String '$s' declared above
}
答案 0 :(得分:1)
假设您的查询有效,看起来可能是:
$did=1;
$yr=2011;
$s='';
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";
$r=mysql_query($q);
while($rw=mysql_fetch_assoc($r))
{
$s .= $rw['Hid'];
}
这只会给你一个包含所有Hids的字符串..如果你想在中间或其他地方有一个角色,你可以:
$did=1;
$yr=2011;
$s=array();
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";
$r=mysql_query($q);
while($rw=mysql_fetch_assoc($r))
{
$s[] = $rw['Hid'];
}
$result = implode( ',', $s );
上面的$ result将以逗号分隔的Hids列表结束。
答案 1 :(得分:0)
使用string concatenation operator:
$s .= $rw['Hid'];
答案 2 :(得分:0)
获取/检索数据到数组并将其内嵌到字符串中。参考 http://php.net/manual/en/function.implode.php
for implode
答案 3 :(得分:0)
$did=1;
$yr=2011;
$s='';
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";
$r=mysql_query($q);
while($rw=mysql_fetch_assoc($r))
{
$s .= $rw['hid'] . '<br />';
}
echo $s;