[英]Extract multiple rows with same id mysql php
Hi I have the following table: 嗨,我有下表:
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
I have the following query: 我有以下查询:
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);
}
When I call this function, it returns only the first row where request_id=3
how to extract all the values returned by the query? 当我调用此函数时,它仅返回第一行,其中
request_id=3
如何提取查询返回的所有值?
This is what I have tried: 这是我尝试过的:
$coach_request_assignment_data = get_coach_request_assignment_on_request_id($request_id);
$coach_request_assignment_data = $coach_request_assignment_data->fetch_assoc();
That's how the var_dump($coach_request_assignment_data)
looks like: 这就是
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)
Thanks here... 谢谢这里...
If this is a MySQLi
API, then you'll need multiple invocations of fetch_assoc()
in order to get those multiple rows. 如果这是
MySQLi
API,则需要多次调用fetch_assoc()
才能获得这些多行。 You have only invoked ->fetch_assoc()
once: 您仅调用
->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);
I'm guessing you're using the mysqli extension? 我猜您正在使用mysqli扩展名?
What about trying: 尝试一下:
$coach_request_assignment_data->fetch_all(MYSQLI_ASSOC);
This will fetch the entire result set at once, rather than needing to loop through it yourself. 这将立即获取整个结果集,而不需要自己遍历它。 The MYSQLI_ASSOC tells it to return it in an associative array with the column names included.
MYSQLI_ASSOC告诉它在包含列名称的关联数组中返回它。 The default is MYSQLI_NUM, which simply returns a numerical-indexed array with no column names.
默认值为MYSQLI_NUM,它仅返回没有列名的数字索引数组。
Are you using PDO::query() ? 您在使用PDO :: query()吗? If yes, query() returns a PDOStatement which you can loop over, eg :
如果是,则query()返回一个PDOStatement,您可以对其进行循环,例如:
$results = get_coach_request_assignment_on_request_id($request_id);
foreach ($results as $result) {
//do something with $result
}
Doc : http://php.net/manual/fr/pdo.query.php Doc: http : //php.net/manual/fr/pdo.query.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.