0

Приветствую!
Подскажите, кто знает, как с помощью условий выбирать определенные значения из базы?
Имею ввиду что-то вроде этого:


$sth = $dbh->prepare("select * from ".DB." where `opt`='1' //IF(!!$g) { and `g`=':g' } //IF(!!$year) { and `year` in (:year) } //IF(!!$id) { and `id` in (:id) } ");

Или как-то так, но это не работает:

$g= 'Alex';
$year= '2013,2014';
$id= '1,2,3';
$sth = $dbh->prepare("select * from ".DB." where `opt`='1' and `g`=':g' and `year` in (:year) and `id` in (:id)");
if(!!$g)
{
$stmt->bindParam(':g', $g);
}
if(!!$year)
{
$stmt->bindParam(':year', $year);
}
if(!!$id)
{
$stmt->bindParam(':id', $id);
}
$stmt->execute();

Обновление

Пока что нашел не совсем правильное, но решение:

if(!!$g)
{
$q_g = " and `g` = $g";
}
if(!!$year)
{
$q_y = " and `year` in ($year)";
}
if(!!$id)
{
$q_q = " and `id` in ($id)";
}
$query = $q_g.$q_y.$q_q;

$sth = $dbh->prepare("select * from ".DB." where `opt`='1'$query");
2
  • Так? (field = :value or :value is null)... 19 сен 2014 в 19:37
  • Делаю так, не срабатывает and (g like '%{$g}%' or g is null)
    – Cript
    21 сен 2014 в 1:47

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.