“參數(shù)號無效:未定義參數(shù)”插入數(shù)據(jù)UPDATE列出VALUES時(shí)我犯了一個小錯誤。我應(yīng)該輸入“:username”而不是“:alias”。我想這個問題的答案可歸功于任何想要它的人的自由統(tǒng)治嗎?或者我刪除這個問題?原版的我一直在使用Yii的活躍記錄模式?,F(xiàn)在,我的項(xiàng)目需要為一個小事務(wù)訪問不同的數(shù)據(jù)庫。我認(rèn)為Yii的DAO對此有好處。但是,我收到了一個神秘的錯誤。CDbCommand無法執(zhí)行SQL語句:SQLSTATE [HY093]:參數(shù)號無效:未定義參數(shù)這是我的代碼:public function actionConfirmation{
$model_person = new TempPerson();
$model = $model_person->find('alias=:alias',array(':alias'=>$_GET['alias']));
$connection=Yii::app()->db2;
$sql = "INSERT INTO users (username, password, ssn, surname
, firstname, email, city, country)
VALUES(:alias, :password, :ssn, :surname
, :firstname, :email, :city, :country)";
$command=$connection->createCommand($sql);
$command->bindValue(":username", $model->alias);
$command->bindValue(":password", substr($model->ssn, -4,4));
$command->bindValue(":ssn", $model->ssn);
$command->bindValue(":surname", $model->lastName);
$command->bindValue(":firstname", $model->firstName);
$command->bindValue(":email", $model->email);
$command->bindValue(":city", $model->placeOfBirth);
$command->bindValue(":country", $model->placeOfBirth);
$command->execute();
$this->render('confirmation',array('model'=>$model));}這構(gòu)造了以下查詢(如應(yīng)用程序日志中所示):INSERT INTO users (username, password, ssn, surname, firstname, email , city, country) VALUES(:alias, :password, :ssn, :surname, :firstname, :email, :city, :country);FYI $model->placeOfBirth應(yīng)該是城市和縣的價(jià)值觀。這不是一個錯字(我只是一件傻事)。
3 回答

蕭十郎
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個贊
上面沒有提到的這個錯誤的原因是當(dāng)你處理一個動態(tài)的參數(shù)數(shù)組時(shí),如果你取消設(shè)置任何參數(shù),你需要在傳入它們之前重新索引。這個的殘酷部分是你的錯誤日志沒有顯示索引,看起來一切都是正確的。例如:
SELECT id WHERE x = ?, y = ?, z = ?
可能產(chǎn)生Log:無效的參數(shù)號:參數(shù)未定義為params(“x”,“y”,“z”)
這看起來不應(yīng)該拋出錯誤,但如果索引是這樣的:
0 => x, 1 => y, 4 => z
它認(rèn)為最后一個參數(shù)未定義,因?yàn)樗趯ふ益I2。

FFIVE
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個贊
可能是你試圖用單引號綁定一個參數(shù)而不是讓它為你工作。
相比:
Model::model()->findAll("t.description ilike '%:filter%'", array(':filter' => $filter));
附:
Model::model()->findAll("t.description ilike :filter", array(':filter' => '%' . $filter . '%'));
添加回答
舉報(bào)
0/150
提交
取消