您好我有下表:
id request_id ein intervention_date time_slot
10 1 600545383 18/05/2015 00:00 AM
11 3 802877145 01/06/2015 00:00 AM
12 3 802310154 02/06/2015 00:00 PM
13 3 801777170 03/06/2015 00:00 AM
我有以下查询:
function get_coach_request_assignment_on_request_id($request_id){
$connection = get_db();
$query = 'SELECT * FROM coach_request_assignment WHERE request_id='.$request_id;
return $connection->query($query);
}
当我调用此函数时,它只返回第一行request_id=3
如何提取查询返回的所有值?
这就是我的尝试:
$coach_request_assignment_data = get_coach_request_assignment_on_request_id($request_id);
$coach_request_assignment_data = $coach_request_assignment_data->fetch_assoc();
var_dump($coach_request_assignment_data)
看起来如何:
array (size=5)
'id' => string '11' (length=2)
'request_id' => string '3' (length=1)
'coach_uin' => string '802877145' (length=9)
'date_intervention' => string '2015-06-01 00:00:00' (length=19)
'time_slot' => string 'AM' (length=2)
在此感谢...
答案 0 :(得分:3)
如果这是MySQLi
API,那么您需要多次调用fetch_assoc()
才能获得这些多行。您只调用->fetch_assoc()
一次:
$results = get_coach_request_assignment_on_request_id($request_id);
$coach_data = array();
while($row = $results->fetch_assoc()) {
$coach_data[] = $row; // push row
}
// print_r($coach_data);
答案 1 :(得分:2)
我猜你正在使用mysqli扩展程序?
尝试怎么样:
$coach_request_assignment_data->fetch_all(MYSQLI_ASSOC);
这将立即获取整个结果集,而不是需要自己循环遍历它。 MYSQLI_ASSOC告诉它在包含列名的关联数组中返回它。默认值为MYSQLI_NUM,它只返回一个没有列名的数字索引数组。
答案 2 :(得分:0)
您使用的是PDO :: query()吗?如果是,query()返回一个可以循环的PDOStatement,例如:
$results = get_coach_request_assignment_on_request_id($request_id);
foreach ($results as $result) {
//do something with $result
}