SELECT * FROM(tablename)WHERE(数组值)=' 1'

时间:2016-05-21 07:26:51

标签: php mysql arrays pdo

我有关于SQL查询的问题。

这是:

$allowed = array("Amsterdam", "Arnhem", "Assen", "Groningen", "Leeuwarden", "Rotterdam", "Sittard", "Tilburg", "Utrecht");

$filter = $db->prepare("SELECT * FROM users WHERE '$allowed' = '1'");
$filter->execute;

foreach($filter->fetchAll(PDO::FETCH_OBJ) as $filtered):

    $amsterdam = $filtered->amsterdam;
    $arnhem = $filtered->arnhem;
    $assen = $filtered->assen;
    $groningen = $filtered->groningen;
    $leeuwarden = $filtered->leeuwarden;
    $rotterdam = $filtered->rotterdam;
    $sittard = $filtered->sittard;
    $utrecht = $filtered->utrecht;

endforeach;

输出(回显时)为空白。

如何让查询从数组中查找列名?

感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

这对我有用:

$ query = $ db-> prepare(" SELECT * FROM tbl_users WHERE id =' {$ userId}'");         $查询 - >执行();

    foreach($query->fetchAll(PDO::FETCH_OBJ) as $value){  

        $districts = array("amsterdam","arnhem","assen","groningen","leeuwarden","rotterdam","sittard","tilburg","utrecht","antillen","missie","dap","sap");
        $arrlength = count($districts);

        for($x = 0; $x < $arrlength; $x++) {

            if($value->$districts[$x] == TRUE){
                echo '<pre>';
                echo ucfirst($districts[$x]) . ' --> ' . $value->$districts[$x];
                echo '</pre>';
            }
        }
    }