带有变量数组的准备语句
Prepared Statement with variable array
我可能想在我的数据库中查找一组字段:
$locations = array(
2,3,5
);
位置的数量可能不同,我想创建一个准备好的语句(在 Symfony2 中使用 Doctrine),其作用与此相同:
SELECT * FROM locations WHERE start = 1 AND (end = 2 OR end = 3 OR end = 5)
(我想)我知道如何使用准备好的语句,像这样:
$query = $em->createQuery(
'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND (l.end = :end1
OR l.end = :end2
OR l.end = :end3)
ORDER BY l.duration ASC'
)
->setParameter('start', '1')
->setParameter('end1', '2')
->setParameter('end2', '3')
->setParameter('end3', '5');
但是如果我没有数组的大小,我该如何动态地做到这一点?
感谢任何提示!
<?php
$locations = array(
2,3,5
);
$query = $em->createQuery(
'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND l.end IN :end
ORDER BY l.duration ASC'
)
->setParameter('start', '1');
->setParameter('end', $locations);
?>
@Christian 的回答很好,但为了完成,您可以使用 foreach
来创建 SQL 字符串。
<?php
$locations = array(2, 3, 5);
$endQueries = [];
foreach ($locations as $i => $location) {
$endQueries[] = "l.end = :end{$i}";
}
$queryString = 'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND (' . implode(' OR ', $endQueries) . ')
ORDER BY l.duration ASC';
$query = $em->createQuery($queryString);
$query->setParameter("start", '1');
foreach ($locations as $i => $location) {
$query->setParameter("end{$i}", $location);
}
也许您缺少 fetchAll?
<?php
$locations = array(
2,3,5
);
$query = $em->createQuery(
'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND l.end IN :end
ORDER BY l.duration ASC'
)
->setParameter('start', '1');
->setParameter('end', $locations);
/* == Added a fetchAll call == */
$query->execute->fetchAll();
?>
我可能想在我的数据库中查找一组字段:
$locations = array(
2,3,5
);
位置的数量可能不同,我想创建一个准备好的语句(在 Symfony2 中使用 Doctrine),其作用与此相同:
SELECT * FROM locations WHERE start = 1 AND (end = 2 OR end = 3 OR end = 5)
(我想)我知道如何使用准备好的语句,像这样:
$query = $em->createQuery(
'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND (l.end = :end1
OR l.end = :end2
OR l.end = :end3)
ORDER BY l.duration ASC'
)
->setParameter('start', '1')
->setParameter('end1', '2')
->setParameter('end2', '3')
->setParameter('end3', '5');
但是如果我没有数组的大小,我该如何动态地做到这一点?
感谢任何提示!
<?php
$locations = array(
2,3,5
);
$query = $em->createQuery(
'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND l.end IN :end
ORDER BY l.duration ASC'
)
->setParameter('start', '1');
->setParameter('end', $locations);
?>
@Christian 的回答很好,但为了完成,您可以使用 foreach
来创建 SQL 字符串。
<?php
$locations = array(2, 3, 5);
$endQueries = [];
foreach ($locations as $i => $location) {
$endQueries[] = "l.end = :end{$i}";
}
$queryString = 'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND (' . implode(' OR ', $endQueries) . ')
ORDER BY l.duration ASC';
$query = $em->createQuery($queryString);
$query->setParameter("start", '1');
foreach ($locations as $i => $location) {
$query->setParameter("end{$i}", $location);
}
也许您缺少 fetchAll?
<?php
$locations = array(
2,3,5
);
$query = $em->createQuery(
'SELECT l
FROM AppBundle:Location l
WHERE l.start > :start
AND l.end IN :end
ORDER BY l.duration ASC'
)
->setParameter('start', '1');
->setParameter('end', $locations);
/* == Added a fetchAll call == */
$query->execute->fetchAll();
?>