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 string)。

第一个转换为字符串,第二个转换为int。将INSERT查询更改为read:

$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";`