在php中执行此操作的最佳方法

在php中执行此操作的最佳方法,php,Php,我从数据库中提取数据,假设一个表中有23列,那么我必须写23行来设置值。 还有其他捷径吗 在下面的例子中,只有15列,我写了15行。有近路吗 while( $row = mysql_fetch_array( $export ) ) { $line = ''; $line.=setValue($row[0]); $line.=setValue($row[1]); $line.=setValue($row[2]);

我从数据库中提取数据,假设一个表中有23列,那么我必须写23行来设置值。 还有其他捷径吗

在下面的例子中,只有15列,我写了15行。有近路吗

while( $row = mysql_fetch_array( $export ) )
    {
        $line = '';
        $line.=setValue($row[0]);
        $line.=setValue($row[1]);
        $line.=setValue($row[2]);
        $line.=setValue($row[3]);
        $line.=setValue($row[4]);
        $line.=setValue($row[5]);
        $line.=setValue($row[6]);
        $line.=setValue($row[7]);
        $line.=setValue($row[8]);
        $line.=setValue($row[9]);
        $line.=setValue($row[10]);
        $line.=setValue($row[11]);
        $line.=setValue($row[12]);
        $line.=setValue($row[13]);
        $line.=setValue($row[14]);

    }

我不确定这是否是您想要的,但您可以使用如下for循环:

while( $row = mysql_fetch_array( $export ) )
    {
        $line = '';
        for($i = 0; $i < 15; $i++)
        {
           $line.=setValue($row[$i]);
        }

    }
while($row=mysql\u fetch\u array($export))
{
$line='';
对于($i=0;$i<15;$i++)
{
$line.=setValue($row[$i]);
}
}

我不确定这是否是您想要的,但您可以使用如下的for循环:

while( $row = mysql_fetch_array( $export ) )
    {
        $line = '';
        for($i = 0; $i < 15; $i++)
        {
           $line.=setValue($row[$i]);
        }

    }
while($row=mysql\u fetch\u array($export))
{
$line='';
对于($i=0;$i<15;$i++)
{
$line.=setValue($row[$i]);
}
}
while($row=mysql\u fetch\u array($export))
{
$line='';
对于($i=0;$i<15;$i++){
$line.=setValue($row[$i]);
}
}
这是一种方法。还有其他的。您可以将“15”替换为返回数组中的计数。这样,for循环将迭代到数组的末尾

Ups,lol,太晚了…

while($row=mysql\u fetch\u array($export))
{
$line='';
对于($i=0;$i<15;$i++){
$line.=setValue($row[$i]);
}
}
这是一种方法。还有其他的。您可以将“15”替换为返回数组中的计数。这样,for循环将迭代到数组的末尾


Ups,lol,太晚了…

而你可以做一些像

while ($row=mysql_fetch_array($export)) {
   $line=array();
   $line=array_map('setValue',$xport);
   $line=implode('',$line);
}

..这是一个非常混乱的构造-它假定查询将返回一个定义非常好的resultset,并且resultset的结构及其后续应用程序被替换;一般来说,你不应该使用mysql\u fetch\u数组,而应该使用mysql\u fetch\u assoc

while ($row=mysql_fetch_array($export)) {
   $line=array();
   $line=array_map('setValue',$xport);
   $line=implode('',$line);
}

..这是一个非常混乱的构造-它假定查询将返回一个定义非常好的resultset,并且resultset的结构及其后续应用程序被替换;作为一般的经验法则,您永远不应该使用mysql\u fetch\u数组-而是使用mysql\u fetch\u assoc。

在循环中使用固定边界是混乱的-在每次迭代中计算计数是无效的$bound=sizeof($row);对于($i=0;$i<$bound;$i++)。。。不过你的方式更好;)当超过15个科伦人时会发生什么?@Shakti Singh:这只是一个例子。。。您可以使用下面的更正或使用其他建议。在循环中使用固定边界是混乱的-在每次迭代中计算计数是innfefficent$bound=sizeof($row);对于($i=0;$i<$bound;$i++)。。。不过你的方式更好;)当超过15个科伦人时会发生什么?@Shakti Singh:这只是一个例子。。。您可以使用下面的更正或使用其他建议。