如果我想从同一个表中获得100个不同的用户,我将不得不创建100个不同的查询,如下所示。我只是以三个查询为例,但你明白了我的意思。
我想知道是否有更有效的方法从同一个表中获得尽可能多的用户?重要的是我需要让每个用户都有自己独特的句柄。例如。 $ matrix_user_id_2,$ matrix_user_id_3,$ matrix_user_id_4。我怎么能继续这样做呢?
$find_sponsor_2 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_2->bindValue(':user_id', 2);
$find_sponsor_2->execute();
$result_sponsor_2 = $find_sponsor_2->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_2) > 0) {
foreach($result_sponsor_2 as $row) {
$matrix_user_id_2 = $row['user_id'];
$filled_positions_2 = $row['filled_positions'];
}
} else {
$errors[] = 'User Id 2 not found in Matrix.';
}
$find_sponsor_3 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_3->bindValue(':user_id', 3);
$find_sponsor_3->execute();
$result_sponsor_3 = $find_sponsor_3->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_3) > 0) {
foreach($result_sponsor_3 as $row) {
$matrix_user_id_3 = $row['user_id'];
$filled_positions_3 = $row['filled_positions'];
}
} else {
$errors[] = 'User Id 3 not found in Matrix.';
}
$find_sponsor_4 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_4->bindValue(':user_id', 4);
$find_sponsor_4->execute();
$result_sponsor_4 = $find_sponsor_4->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_4) > 0) {
foreach($result_sponsor_4 as $row) {
$matrix_user_id_4 = $row['user_id'];
$filled_positions_4 = $row['filled_positions'];
}
} else {
$errors[] = 'User Id 4 not found in Matrix.';
}
答案 0 :(得分:0)
您可以使用1个查询获取所有信息。你为什么不这样做?
SELECT user_id, filled_positions FROM matrix_2 WHERE user_id in (2,3,4) // your ids here
然后你可以迭代结果集来检查哪一个存在而哪一个不存在。