【问题标题】:How to insert php variables into oracle table?如何将php变量插入oracle表?
【发布时间】:2015-02-16 19:31:58
【问题描述】:

我有一些 php 变量想要插入到 oracle 表中,但我很难使用转义引号。

这是我目前所拥有的:

   <?php
     ......
     $number_passed=20;//this is calculated earlier in the code
     $number_total=100;//also calculated earlier in the code
     $date=date('m/d/y');
     $username=//username here
     $password=//password here
     $database=//database connection string here

     $connection=oci_connect($username,$password,$database);

     $sql="INSERT INTO TEST_TABLE (Date_Col,num_pass,num_total) 
               VALUES ('"$date"','"$number_passed"','"$number_total"')";

     $st= oci_parse($$connection, $sql);
     oci_execute($st);

    ?>

当我这样做时,我收到以下错误:解析错误:语法错误,意外的 T_VARIABLE 在我声明我的 sql 语句的那一行。如何将php变量正确插入到数据库表中?

另外,我知道我应该在将我的 php 变量插入数据库之前对其进行清理。有没有一个函数可以为我做到这一点?

谢谢!

【问题讨论】:

  • 使用参数化查询而不是串联。
  • $st= oci_parse($$connection, $sql);到 $st= oci_parse($connection, $sql);
  • 感谢您发现错误。

标签: php database oracle oracle-call-interface


【解决方案1】:

简单的字符串连接问题。

  VALUES ('${date}','${number_passed}','${number_total}')";

甚至不需要逃避解释器。

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 2020-10-29
    相关资源
    最近更新 更多