2017-07-26 38 views
3

我想选择状态为review(其中有四个:sky - earth - sea - sun)的所有用户值。获取有关另一列中的值的值

$stmt = $db->query("select user from posts where status = 'review'"); 
$rows = $stmt->fetch(); 
foreach ($rows as $row){ 
    echo $row . '<br>'; 
} 

结果:

sky 
sky 

我需要:

sky 
earth 
sea 
sun 

任何帮助吗?

+0

@vasek,我不能看出其中的差别比较,以我的查询! – bonaca

+0

@vasek有什么区别? –

+0

@vasek,结果相同。顺便说一句,所有四个值已经不同。 – bonaca

回答

2

既然你要查询的多个记录。 $rows=$stmt->fetch()一次只能得到一条记录,因此您需要使用while循环来获取与您的鳕鱼相匹配的每条记录。

试试这个:

while ($rows = $stmt->fetch()) { 
    echo $rows['user'] . '<br>'; 
} 
2
$stmt = $db->query("select user from posts where status = 'review'"); 
while ($rows = $stmt ->fetch_object()) { 
    echo $rows->user . '<br>'; 
} 

这里更新答案

+0

'注意:数组到字符串转换...' – bonaca

+0

while($ rows = $ stmt-> fetch()){ echo $ rows-> user。 '
'; } – sunilwananje

+0

答案已经更新。 – localheinz