简体   繁体   English

foreach循环数组sql查询

[英]foreach loop array sql query

I'm trying to select values from my database when the value != 0 but all I get is an empty array. 我正在尝试从数据库中选择值!= 0时的值,但我得到的只是一个空数组。

Here I've got an array with all the names who are also in the database. 在这里,我得到了一个包含所有也在数据库中的名称的数组。

$array  = ['helm' , 'weapon', 'amulet', 'body', 'legs', "boots", 'shield', 'gloves', 'cape'];

then I place the array in a foreach loop and place the _name and _value behind the name (in the database it is helm_name = blabla _helm value = 10 ) 然后将数组放在foreach循环中,并将_name_value放在名称后面(在数据库中为helm_name = blabla _helm value = 10

When the text is placed behind the word I loop through it in my query in which I try to select the name table (for example helm_name ) and the value table (for example helm_value) 当文本放在单词的后面时,我在query中遍历该单词,在query中尝试选择name table (for example helm_name )和value table (for example helm_value)

foreach($array as $array_item){
    $array_item_name = $array_item.'_name';
    $array_item_value = $array_item.'_value';

    $sql = $db->prepare("SELECT ".$array_item_name." FROM player WHERE naam = :naam AND ".$array_item_value." != 0");
    $sql->execute(array(":naam" => $naam));
    $fetch = $sql->fetchAll(PDO::FETCH_ASSOC);
}

but all it returns when I run it is Array ( ) what am I doing wrong? 但是我运行时返回的全部是Array ( )我在做什么错?

请检查数据库错误值,也应首先创建sql字符串并输出它,以便可以手动运行生成的sql语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM