[英]SQL Query Works, But Not In PHP
我測試並成功使用下面的SQL代碼將數據輸入數據庫。
INSERT INTO hraps (id, firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter) values(111111, 'World', 'Hello', 'Male', '2007', '1', '2', '0')
現在,我試圖像這樣將其集成到PHP中:
$query = "INSERT INTO hraps (firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter) "
."values('".$this->firstname."','".$this->lastname."','".$this->gender."','".$this->year_of_1st_rappel."',".$this->count_offset_proficiency.",".$this->count_offset_operational.",".$this->spotter.") returning id into :id";
$dbid = "";
$binds = array();
$binds[] = array("name" => ":id", "value" => &$dbid, "length" => 128);
//echo $query;
$result = mydb::cxn()->query($query, $binds);
$this->id = $dbid;
但是什么也沒插入,我沒有任何錯誤。 唯一的區別是,在此代碼中,我將id定義為$ dbid,並且在將其硬編碼到查詢的“ values”部分中之前。
有人可以指出為什么此代碼無法成功工作嗎? 謝謝。
只需刪除"."
在VALUES
之前。 嘗試這個
而您錯過了mysqli_query()
$query= "INSERT INTO hraps (firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter)
values ('".$this->firstname."','".$this->lastname."','".$this->gender."','".$this->year_of_1st_rappel."',".$this->count_offset_proficiency.",".$this->count_offset_operational.",".$this->spotter.") returning id into :id ";
編輯:如果它Oracle然后使用此
$compiled = oci_parse($db, $query); //-- $db is your connection to database variable
oci_bind_by_name($compiled, ':id', $id); // --your id
oci_execute($compiled);
上面的兩個查詢不相等,因為它們使用不同的數據類型(int v字符串)。
第一個強制轉換為字符串,第二個強制轉換為int。 將INSERT查詢更改為:
$query = "INSERT INTO hraps (firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter) "
."values('".$this->firstname."','".$this->lastname."','".$this->gender."','".$this->year_of_1st_rappel."','".$this->count_offset_proficiency."','".$this->count_offset_operational."','".$this->spotter."') returning id into :id";`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.