如何使用PHP从mySQLi查询中回显每一行? [英] How to echo each row from mySQLi query using PHP?

查看:82
本文介绍了如何使用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.

这篇关于如何使用PHP从mySQLi查询中回显每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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