SQL查询工作,但不是PHP

时间:2013-02-08 22:23:19

标签: php sql oracle

我测试并成功使用下面的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,之后我在查询的“值”部分对其进行了硬编码。

有人可以指出为什么这段代码无法成功运行? 谢谢。

2 个答案:

答案 0 :(得分:1)

"."之前删除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);

答案 1 :(得分:0)

上面的两个查询不相等,因为它们使用不同的数据类型(int v string)。

第一个转换为字符串,第二个转换为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";`