douwen0612 2016-12-22 16:54
浏览 123
已采纳

php foreach循环不会遍历整个数组

Test driving the following script. php 5.3.3. mysql 5.0.95.

    <?php 
// show errors in browser
error_reporting(E_ALL);
ini_set('display_errors', 'on');
// connect to database
$conn = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
if (!$conn) {
    die("connection failed: " . mysqli_connect_error());
}
$company_request = 'xxx';
$employee_request = 'xxx';
$timecard_data_results = array();
$fill_old_data_array_def = " SELECT company_id, employee_id, location, task_name, task_start_time, task_end_time, tccomment FROM timecard WHERE company_id = '" . $company_request . "' AND employee_id = '" . $employee_request . "' ORDER BY task_start_time";
$timecard_data_results = mysqli_query($conn, $fill_old_data_array_def);
$iteration = '1';
foreach ($timecard_data_results as $timecard_record) {
    $timecard_record = mysqli_fetch_assoc($timecard_data_results);
    echo $timecard_record['company_id'] . $timecard_record['task_name'] . $timecard_record['task_start_time'];

    echo "  " . $iteration . "<br>";
    $iteration = $iteration + '1';
}
echo "<br>" . $iteration . "<br>";
echo '<pre>';
print_r($timecard_data_results);
echo '</pre>';
?>

Browser output is:

HOSCS:Train Crew:Engineer trainin 2016-10-14 07:00:00 1
HOSCS:Train Crew:Engineer in trai 2016-10-16 10:00:00 2
HOSCS:Train Crew:Engineer in trai 2016-10-17 07:30:00 3
HOSCS:Train Crew:Engineer in trai 2016-10-18 08:15:00 4
HOSCS:Train Crew:Engineer in trai 2016-10-19 09:45:00 5

6
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 7
    [lengths] => Array
        (
            [0] => 3
            [1] => 6
            [2] => 21
            [3] => 31
            [4] => 19
            [5] => 19
            [6] => 1
        )

    [num_rows] => 49
    [type] => 0
)

Foreach loop appears to quit after five passes. No error messages in browser. Does not appear that script was aborted. Print_r shows 49 rows in the array. I've tried different sort order in the query on the chance some data was antagonizing the loop. Same result: Five records. Query takes .062 seconds. Colons in output are part of fields from original text file architecture that will need to be parsed in foreach loop once it will run through all rows. Stumped.

  • 写回答

2条回答 默认 最新

  • duan1979768678 2016-12-22 17:04
    关注

    The mysqli_result class didn't implement Traversable until PHP 5.4. In PHP 5.3 you will need to use a while loop.

    <?php 
    // show errors in browser
    error_reporting(E_ALL);
    ini_set('display_errors', 'on');
    // connect to database
    $conn = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
    if (!$conn) {
        die("connection failed: " . mysqli_connect_error());
    }
    $company_request = 'xxx';
    $employee_request = 'xxx';
    $timecard_data_results = array();
    $fill_old_data_array_def = " SELECT company_id, employee_id, location, task_name, task_start_time, task_end_time, tccomment FROM timecard WHERE company_id = '" . $company_request . "' AND employee_id = '" . $employee_request . "' ORDER BY task_start_time";
    $timecard_data_results = mysqli_query($conn, $fill_old_data_array_def);
    $iteration = '1';
    
    while($timecard_record = mysqli_fetch_assoc($timecard_data_results))
        echo $timecard_record['company_id'] . $timecard_record['task_name'] . $timecard_record['task_start_time'];
    
        echo "  " . $iteration . "<br>";
        $iteration = $iteration + '1';
    
    
    }
    echo "<br>" . $iteration . "<br>";
    echo '<pre>';
    print_r($timecard_data_results);
    echo '</pre>';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。