如何使用PHP从mySQLi查询中回显每一行?
问题描述:
以下查询返回广告系列表中所有user_id为1的行:
The following query returns all rows from the campaign table that have the user_id of 1:
SELECT * FROM campaign WHERE user_id=1
在测试的情况下,这是两个结果.我如何能够从每一行中回显设置的列.例如,我想从每个结果中回显campaign_name.我尝试了各种方法,但是没有成功.
In the case of testing this is two results. How would I be able to echo a set column from each of the rows. For example, I want to echo the campaign_name from each of the results. I have tried various methods however, I have had no success.
我的最终目标将是这样:
My end goal would be something like this:
<?php foreach($queryRow as $row) { ?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php } ?>
我对此一无所知,如果我的预期结果完全不正确,我深表歉意...
I'm quite at lost with this so I apologise if my intended result is completely off...
答
尝试一下:
$qry = "SELECT * FROM campaign WHERE user_id=1";
$res = mysqli_query($conn, $qry);
if(mysqli_num_rows($res) > 0) // checking if there is any row in the resultset
{
while($row = mysqli_fetch_assoc($res)) // Iterate for each rows
{
?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php
}
}
它将对结果集中的每一行进行迭代.
It will iterate for each row in the resultset.