drevls8138 2016-02-09 04:51
浏览 130
已采纳

将PHP变量传递给oci_parse中的sql查询

I am passing a PHP varibale into a oracle sql query. but its not taking it properly giving me ORA errors like - invalid character. I tried escaping the varibale as \'$sid\', this makes error go, but the query doesnt return anything. Is there a way to pass PHP variable to oracle query

if(isset($_POST['action']))
{
   $sid = $_POST['action'];
   $stid = oci_parse($conn, 'SELECT emp from table emp='$sid'');
   oci_execute($stid);
}

I have removed to the database connection part for brevity.

  • 写回答

1条回答 默认 最新

  • douying6206 2016-02-09 05:49
    关注

    'SELECT emp from table emp=\'$sid\'' is a string that you pass exactly as it is to Oracle, this is why it doesn't work.

    You need to use oci_bind_by_name to bind a placeholder to a PHP variable.

    Example:

    $variable = 42;
    $stid = oci_parse($conn, 'SELECT col_name FROM tbl_name WHERE col_name > :num;');
    oci_bind_by_name($stid, ":num", $variable);
    oci_execute($stid);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?