我PDO類有一個叫做bindParams功能,其中有2個參數:setValues方法和的setType,自動分配一個PDO型
輸出的結果應該是這樣的:
$insertNews->bindParam(':news_title', $newsTitle, PDO::PARAM_STR);
所以,我期待自動分配 「PDO :: PARAM_STR」 他們的價值觀,這是在我的情況 「news_title」 是變量setValues方法,而 「PDO :: PARAM_STR」 是的setType:
public final function bindParams($setValues=array(), $setType = null){
//print_r($setValues);
foreach ($setValues as $getVal) {
echo $getVal.'<br />';
if (is_null($setType)) {
switch ($getVal) {
case is_int($getVal):
echo $getVal.' is INT<br />';
$setType = PDO::PARAM_INT;
break;
case is_bool($getVal):
echo $getVal.' is BOOL<br />';
$setType = PDO::PARAM_BOOL;
break;
case is_null($getVal):
echo $getVal.' is NULL<br />';
$setType = PDO::PARAM_NULL;
break;
default:
echo $getVal.' is STR<br />';
$setType = PDO::PARAM_STR;
}
}
}
} // end bindParams()
$con = new crud($dbCon);
$con->insert('ban_ip', array('visitor_type', 'ip'));
$con->bindParams(array('Visitor_Type', 1));
輸出結果是:
VISITOR_TYPE是STR
這不是循環的其他價值,這是1
編輯:正確的代碼:
我認爲這一個工程:
public final function bindParams($setValues=array(), $setType = null){
$combine = array_combine($this->insertedKeys, $setValues);
foreach ($combine as $getKey => $getVal) {
//echo 'key '.$getKey.' val '.$getVal.'<br />';
switch ($getVal) {
case is_int($getVal):
echo $getVal .' is INT<br />';
$setType = 'PDO::PARAM_INT';
break;
case is_bool($getVal):
$setType = 'PDO::PARAM_BOOL';
echo $getVal .' is BOOL<br />';
break;
case is_null($getVal):
echo $getVal .' is NULL<br />';
$setType = 'PDO::PARAM_NULL';
break;
default:
echo $getVal .' is STR<br />';
$setType = 'PDO::PARAM_STR';
break;
}
echo "this->stmt->bindParams($getKey, $getVal, $setType)<br />";
}
} // end bindParams()
結果是:
Visitor_Type is STR
this->stmt->bindParams(visitor_type, Visitor_Type, PDO::PARAM_STR)
1 is INT
this->stmt->bindParams(ip, 1, PDO::PARAM_INT)
如果我沒有弄錯,我應該只執行沒有任何回聲的代碼來運行它。
謝謝您的幫助
使用'gettype'會更好地識別類型並防止任何類型的歧義 - http://php.net/gettype – Clay
是的,我正在尋找4個params:NULL,INT,STR或BOOL,..我從來沒有在我的bindParams中使用其餘的 – Fred