我有以下PHP PDO语句:
$STH = $this->_db->prepare("INSERT INTO UserDetails (FirstName, LastName,
Address, City, County, PostCode, Phone, Mobile, Sex, DOB,
FundraisingAim, WeeksAim, LengthsAim, HearAboutID,
MotivationID, WelcomePackID, ContactPrefID, TitleID)
VALUES
(:firstName, :lastName, :address, :city, :county, :postCode,
:phone, :mobile, :sex, :DOB, :fundraisingAim, :weeksAim,
:lengthsAim, :hearAbout, :motivation,
:welcomePackPref, :contactPref, :title)");
$STH->execute($userData);
其中$userData是一个关联数组.我仔细检查了名称,我不明白为什么我收到以下错误:
SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配
我犯了什么愚蠢的错误?
解决方法:
您的$userData必须具有与您的语句绑定的完全相同的占位符,不多也不少.请参阅PDOStatement::execute documentation,该部分显示“您无法绑定多于指定值的值”.
你需要准备你的参数execute()来准确匹配你的绑定.如果正确排列数组,使用array_intersect_key()很容易.我通常将它包装在一个函数中,它也会处理前缀,如下所示:
// Adds a prefix to a name for a named bind placeholder
function prefix($name) {
return ':'.$name;
}
// like 'prefix()', but for array keys
function prefix_keys($assoc) {
// prefix STRING keys
// Numeric keys not included
$newassoc = array();
foreach ($assoc as $k=>$v) {
if (is_string($k)) {
$newassoc[prefix($k)] = $v;
}
}
return $newassoc;
}
// given a map of datakeyname=>columnname, and a table name, returns an
// sql insert string with named bind placeholder parameters.
function makeInsertStmt($tablename, $namemap) {
$binds = array_map('prefix', array_keys($namemap));
return 'INSERT INTO '.$tablename.' ('.implode(',',$namemap).') VALUES ('
.implode(',',$binds).')';
}
// returns an array formatted for an `execute()`
function makeBindData($data, $namemap) {
// $data assoc array, $namemap name->column mapping
return prefix_keys(array_intersect_key($data, $namemap));
}
// example to demonstrate how these pieces fit together
function RunTestInsert(PDO $pdo, $userData) {
$tablename = 'UserDetails';
// map "key in $userData" => "column name"
// do not include ':' prefix in $userData
$namemap = array(
'firstName' => "FirstName",
'lastName' => "LastName",
'address' => "Address",
'city' => "City",
'county' => "County",
'postCode' => "PostCode",
'phone' => "Phone",
'mobile' => "Mobile",
'sex' => "Sex",
'DOB' => "DOB",
'fundraisingAim' => "FundraisingAim",
'weeksAim' => "WeeksAim",
'lengthsAim' => "LengthsAim",
'hearAbout' => "HearAboutID",
'motivation' => "MotivationID",
'welcomePackPref' => "WelcomePackID",
'contactPref' => "ContactPrefID",
'title' => "TitleID",
);
$sql = makeInsertStmt($tablename, $namemap);
$binddata = makeBindData($userData, $namemap);
$pstmt = $pdo->prepare($sql);
$pstmt->execute($binddata);
}
像这样的抽象的好处是你不需要担心绑定参数本身.
标签:php,arrays,mysql,pdo,associative-array
来源: https://codeday.me/bug/20190923/1814246.html