简体   繁体   English

奇怪的PDO错误hy093

[英]Strange PDO error hy093

First of all, excuse me, if similar questions have been asked before. 首先,请问我是否曾经问过类似的问题。 But I did not find anything that could help me with my problem. 但是我没有找到任何可以帮助我解决问题的东西。

On it goes: In a class, I generate an SQL statement like this: 进行中:在一个类中,我生成一个如下的SQL语句:

        $params = array();
$query = 'INSERT INTO '.TABLE_SETTINGS.' SET type=:type,';
        $params[] = array('type' => $setting['type']);

        if(!isset($setting['attributes'])){
            $query .= 'attributes=NULL,';   
        } else {
            $query .= 'attributes=:attributes,';
            $params[] = array('attributes' => $setting['attributes']);
        }

        if(!isset($setting['partid'])){
            $query .= 'partid=NULL,';   
        } else {
            $query .= 'partid=:partid,';
            $params[] = array('partid' => (int)$setting['partid']);
        }

        if(!isset($setting['description'])){
            $query .= 'description=NULL,';  
        } else {
            $query .= 'description=:description,';
            $params[] = array('description' => addslashes($setting['description']));
        }


        $query .= 'host=:host,name=:name,';
        $params[] = array('host' => (int)$setting['host']);
        $params[] = array('name' => $setting['name']);

        if(!isset($setting['content'])){
            $query .= 'content=NULL,';  
        } else {
            $query .= 'content=:content,';
            $params[] = array('content' => addslashes($setting['content']));
        }

        if(!isset($setting['trigger'])){
            $query .= 'trigger=NULL';   
        } else {
            $query .= 'trigger=:trigger';
            $params[] = array('trigger' => addslashes($setting['trigger']));
        }

After that, I pass it to a database function: dbQuery($query,$params) 之后,我将其传递给数据库函数: dbQuery($ query,$ params)

The function dbQuery relies on a working PDO Connection and goes like this: 函数dbQuery依赖于工作的PDO连接,如下所示:

function dbQuery($query,$params = array()){
global $DBVARS;  // I know that is not very nice ;-)
$db = dbInit();

$prefix = isset($DBVARS['table_prefix']) ? $DBVARS['table_prefix'] : '';
$sql = str_replace("{prefix}",$prefix,$query);

if(isset($params) && is_array($params) && count($params) > 0){
    $q = $db->prepare($sql);
    if(!$q->execute($params)){ // Added this just for debugging purpose
        $q = $q->errorCode();
    }
} else {
    $q = $db->query($sql);
}
$db->num_queries++;

return $q;

} }

If I now var_dump(dbQuery($query,$params)), it returns me the HY093 error, which tells me, that my parameters do not match the tokens, at least that was what I have found via google. 如果现在使用var_dump(dbQuery($ query,$ params)),它会向我返回HY093错误,告诉我我的参数与令牌不匹配,至少那是我通过google找到的。 To make it clear, I show here the complete generated $query: 为了清楚起见,我在这里显示完整的生成的$ query:

INSERT INTO table_name SET type=:type,attributes=NULL,partid=NULL,description=:description,host=:host,name=:name,content=:content,trigger=NULL

And this is the array $params: 这是数组$ params:

array(5) { [0]=> array(1) { ["type"]=> string(5) "yesno" } [1]=> array(1) { ["description"]=> string(79) "If yes, the link to the password score table is shown. It is hidden by default." } [2]=> array(1) { ["host"]=> int(49) } [3]=> array(1) { ["name"]=> string(17) "enable_scoretable" } [4]=> array(1) { ["content"]=> string(3) "yes" } }

I checked it several times, but as far as I can see, the count of my elements matches the count of my array and the names are equal, too. 我检查了好几次,但据我所知,元素的数量与数组的数量匹配,并且名称也相等。 So where do I have the error? 那么我哪里有错误?

I am fairly new to php, and english is not my natve tongue, so I hope I could express myself well enough. 我对php还是很陌生,英语不是我的直言,所以我希望我能很好地表达自己。

Any help with this is gadly appreciated. 对此的任何帮助将不胜感激。 I have been googling for hours without any usable result. 我已经搜寻了几个小时,没有任何可用的结果。 So I decided to post my very first question here. 因此,我决定在这里发布我的第一个问题。

Greetings from Germany and thank you for your help. 来自德国的问候,谢谢您的帮助。

It's not the right syntax for a insert query ! 这不是插入查询的正确语法! Your are confounding with an update query ... 您对更新查询感到困惑...

INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) INSERT INTO table_name(第1栏,第2栏,第3栏...)值(第1值,第2栏,第3栏...)


Edit : 编辑:

$params[] = array('type' => $setting['type']); // Wrong

It's gonna create an array of arrays. 它会创建一个数组数组。

$params = array_merge($params, array('type' => $setting['type'])); 

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

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