简体   繁体   English

为什么echo只从PHP数组中打印一个值?

[英]Why does echo print only one value from PHP array?

I got the following code: 我得到以下代码:

$comments = $db->QueryFetchArray("SELECT * FROM `ycommentbox` WHERE `url`='".$id."'");
foreach ($comments as $comment) {
    echo $comment;
}

which echoes: 呼应:

"test121677129055" “ test121677129055”

"ycomemntbox" structure: “ ycomemntbox”结构:

三排

Why isn't it echoing the other 2 rows but just the first one? 为什么它不回响其他两行,而只是第一行?

It should echo this: 它应回显此:

"test121677129055" “ test121677129055”
"test221677129056" “ test221677129056”
"test321677129057" “ test321677129057”

Because QueryFetchArray only returns a single row; 因为QueryFetchArray仅返回一行; it's only an array because it includes all the column values from that row. 它只是一个数组,因为它包括该行中的所有列值。

To get all the rows, you need to either do QueryFetchAll to get them in one big array, or do an initial Query followed by FetchArray in a loop that will run once per row. 要获取所有行,您需要执行QueryFetchAll以将它们放入一个大数组中,或者执行一个初始Query然后是FetchArray进行循环,该循环每行运行一次。

Try this 尝试这个

And your id wants to match with this query : 并且您的ID要与此查询匹配:

 $comments = $db->QueryFetchAll("SELECT * FROM `ycommentbox` WHERE `url`='".$id."'");
    foreach ($comments as $comment) {
        echo $comment;
    }

OR you can change this query like this 或者您可以像这样更改此查询

$comments = $db->QueryFetchAll("SELECT * FROM `ycommentbox`");
        foreach ($comments as $comment) {
            echo $comment;
        }

You can use print_r for multiple values 您可以将print_r用于多个值
and use echo for single value ok 并使用echo作为单值确定
OR: 要么:
If you use only echo then use echo & tr td html tags between "" . 如果你只使用echo然后使用之间回声&TR TD HTML标签""
Write starting tags in "" and .$variable . "".$variable写入开始标记。 and again end tags between `"" 并再次在“”之间结束标签

For loop looks ok, so I can suggest first to make a {echo count($comments);} to check actually how many rows returned by the query. for循环看起来不错,因此我建议您首先进行{echo count($ comments);},以检查查询返回的实际行数。 It will validate the query first. 它将首先验证查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM