首先,对不起,如果以前有人问过类似的问题。但是我没有找到任何可以帮助我解决问题的方法。
它继续: 在一个类中,我生成如下 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" } }
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.
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.