繁体   English   中英

SQL查询有效,但不适用于PHP

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM