通过PHP数组循环从MySQL查询返回

通过PHP数组循环从MySQL查询返回

本文介绍了通过PHP数组循环从MySQL查询返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过MySQL查询的返回数组的形式循环(大家都知道)的应用程序的一部分。但是,我需要放置在一些返回的项的若干不同格式的设置,例如,一列需要日语货币,另一个有美国货币和返回的项目之一是到图像的链接。

我会使用列的名称,但将被用于许多不同的表,我使用来完成这个这个相同的功能。

这是我为循环为止。

 而($行= mysql_fetch_array($结果)){
    为($ I = 0; $ I< =计数($行); $ I ++){
    如果($行[I] == $行['Yen_Price']){//我没想到这个工作......但是这是我想做些什么。
        回声你好;
    }
    回声< TD>中。 $行[$ i]。 &所述; / TD>中;
    }
}


解决方案

 而($行= mysql_fetch_assoc($结果)){
    的foreach($行为$关键=> $值){
        如果($键=='Yen_Price'){
            回声你好;
        }
        回声< TD> $价值< / TD>中;
    }
}

尽管如此,使用相同的函数来处理所有可能的表中的所有结果将很快而难以管理。你应该自定义此为合适的场合,像这样:

 而($行= mysql_fetch_assoc($结果)){
    回声< TD>富:$行[富] LT; / TD>中;
    回声< TD>酒吧:$行[巴]< / TD>中;
}

I have a part of an application that loops through a return of a MySQL query (as we all know) in the form of an Array. However, I need several different format settings placed on some of the items returned, for example, one column needs Japanese currency, the other has American currency and one of the returned items is a link to an image.

I would use the names of the column, however this same function that I am using to accomplish this will be used for many different tables.

This is what I have for the loop so far.

while($row = mysql_fetch_array($result)) {
    for($i=0;$i<=count($row);$i++) {
    if($row[i]==$row['Yen_Price']) {// I didn't expect this to work...but this is what I would like to do.
        echo "Hello";
    }
    echo "<td>" . $row[$i] . "</td>";
    }
}
解决方案
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $key => $value) {
        if ($key == 'Yen_Price') {
            echo "Hello";
        }
        echo "<td>$value</td>";
    }
}

Having said that, using the same function to process all results from all possible tables will soon be rather unmanageable. You should customized this to fit the occasion like so:

while ($row = mysql_fetch_assoc($result)) {
    echo "<td>Foo: $row[foo]</td>";
    echo "<td>Bar: $row[bar]</td>";
}

这篇关于通过PHP数组循环从MySQL查询返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:16