测试驾驶以下脚本。 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>';
?>
浏览器输出为:
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循环似乎在五次传递后退出。浏览器中没有错误消息。看起来脚本没有中止。 Print_r在数组中显示49行。 我在查询中尝试了不同的排序顺序,以防止某些数据对抗循环。同样的结果:五条记录。 查询需要.062秒。 输出中的冒号是来自原始文本文件体系结构的字段的一部分,一旦它将遍历所有行,就需要在foreach循环中进行解析。 难住了。
答案 0 :(得分:2)
mysqli_result类在PHP 5.4之前没有实现Traversable。在PHP 5.3中,您将需要使用while循环。
<?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 :(得分:0)
你应该使用
next()