【问题标题】:SQL Query Works, But Not In PHPSQL 查询有效,但在 PHP 中无效
【发布时间】:2013-01-24 19:04:30
【问题描述】:

我测试并成功使用下面的这个 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,并且在我将其硬编码到查询的“值”部分之前。

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

【问题讨论】:

  • 第一步:检查您的数据库连接是否正常。我们无法为您执行此操作。
  • 那些查询不相等,第一次插入“1”、“2”和“3”,第二次插入 1,2,3
  • 错误报告是否开启?如果您说出您尝试过的方法,则无需再问这些问题。
  • 就是这样:“警告:array_change_key_case():参数应该是一个数组...”。
  • 执行echo $query; 并检查查询是否是您想要的。为什么你不为你的所有价值观使用准备好的陈述?

标签: php sql oracle


【解决方案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);

【讨论】:

  • 对不起,我应该提到,它不是 mysql 或 mysqli,它是一个 Oracle DB。
【解决方案2】:

上面的两个查询不相等,因为它们使用不同的数据类型(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";`

【讨论】:

  • 另外,尝试回显 DB 错误,仅仅因为 PHP 没有抛出错误并不意味着您的数据库类或数据库本身没有。
  • 这只会使站点崩溃并导致 HTTP 500 错误。不过还是谢谢。
  • 有趣的是,我知道导致 500 次崩溃的唯一数据库错误是无限请求,这通常发生在使用 0 或 false 的主键时。
猜你喜欢
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多