-1

您好,我将从 for 获取数组数据。我有一个代码,但在之前的代码 cs=2 中效果不佳

for($k=0;$k<$cs;$k++)
{ 


$SQL = "SELECT duration FROM core_network WHERE location=('".$location_c[$k]."')";
$result = mysql_query($SQL);


$cks="0";
$duration=array();
while ($db_field = mysql_fetch_array($result)) {
$duration_c[$cks]= $db_field['duration']; //*here must save to data
 $cks++;

} 
 }

我已将此代码保存在 search_l.php

 <?php include("lib/search_l.php"); ?>



  <?php
if($cs!=0){
for($i=0;$i<$cs;$i++)
{ 

?>



 <div id="alarmdisplay">
   <table class width="634" border="0" cellspacing="3" cellpadding="3">
  <tr class="search">
    <td width="256"><?php echo $location_c[$i] ?></td>
    <td width="154" class="247"><?php  echo $duration_c[$i]
?></td>
    <td width="194">&nbsp;</td>
  </tr>
 </table>

在这里它必须是打印 2 持续时间。但它只打印另一个有错误注意:未定义的偏移量:1 如何正确打印它?

当我测试它以检查正确时,我注意到:

$result)) {
$duration_c[$cks]= $db_field['duration'];
 $cks++;

} echo $duration_c[$cks]   //*it prints correct i meant prints 2 test data
}

在那之后

$result)) {
$duration_c[$cks]= $db_field['duration'];
 $cks++;

}  
}
echo $duration_c[$cks]  //*it prints not correct i meant prints only first test data

如何纠正它?

4

4 回答 4

0

好的,然后尝试这样的事情,这将得到你插入数组的结果。

<?php

for($i = 0; $i < count($duration); ++$i) {
    echo $duration[$i];
?> 
于 2012-11-13T08:37:43.540 回答
0

您的代码中有一个简单的类型错误:

$duration=array();

应该

$duration_c=array();

否则这将是范围界定的问题,因为 在你的循环$duration_c之外不会知道。while

于 2012-11-13T08:42:58.367 回答
0
$duration_c=array();//Should be declared outside loop

for($k=0;$k<$cs;$k++)
 { 
  $SQL = "SELECT duration FROM core_network WHERE location=('".$location_c[$k]."')";
  $result = mysql_query($SQL);
  $cks = 0;//String variable
  while ($db_field = mysql_fetch_array($result,MYSQL_ASSOC)) {
                                                    //^---------returns associative array
      $duration_c[$cks]= $db_field['duration'];
      $cks++;
   } 
 }
于 2012-11-13T08:43:22.023 回答
0

如果您想要一个键类型结果(关联数组),请使用

mysql_fetch_assoc();

反而

mysql_fetch_array();
于 2012-11-13T08:32:52.297 回答