【问题标题】:Strange PDO error hy093奇怪的 PDO 错误 hy093
【发布时间】:2013-03-31 00:01:00
【问题描述】:

首先,如果之前有人问过类似的问题,请原谅。但我没有找到任何可以帮助我解决问题的方法。

继续: 在一个类中,我生成如下 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']));
        }

之后,我将它传递给一个数据库函数:dbQuery($query,$params)

函数 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;

}

如果我现在 var_dump(dbQuery($query,$params)),它会返回 HY093 错误,告诉我我的参数与令牌不匹配,至少这是我通过 google 找到的。为了清楚起见,我在这里展示了完整生成的 $query:

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

这是数组 $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" } }

我检查了几次,但据我所知,我的元素数与我的数组数相匹配,并且名称也相等。那么我的错误在哪里?

我是 php 的新手,英语不是我的母语,所以我希望我能很好地表达自己。

非常感谢您对此的任何帮助。我已经搜索了几个小时没有任何可用的结果。所以我决定在这里发布我的第一个问题。

来自德国的问候,感谢您的帮助。

【问题讨论】:

    标签: php pdo


    【解决方案1】:

    这不是插入查询的正确语法! 您对更新查询感到困惑......

    INSERT INTO table_name (column1, column2, column3,...) 值(值1,值2,值3,...)


    编辑:

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

    它将创建一个数组数组。

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

    【讨论】:

    • 好主意,但在这种情况下,我所有的其他插入查询也会失败。上面的语法在 mysql 5 中也是有效的,见dev.mysql.com/doc/refman/5.5/en/insert.html 所以它肯定是别的东西;-)
    • 好吧,不知道!我通常使用 bindParam 方法,但我确定 execute() 不接受数组数组!尝试使用 array_merge, $params= array_merge($params, array('type' =>$setting['type']));
    • 伙计,这当然是问题所在。编写代码可能为时已晚,或者我只是太笨了,看不到。谢谢您的帮助。对于替代的 INSERT 语法:我一直使用它,每当我首先必须检查一个条目是否已经存在并且需要升级时。所以我可以先准备 SET a = b 部分,最后根据存在情况添加 INSERT INTO 或 UPGRADE ;-) 非常感谢您的帮助!
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多