简体   繁体   English

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

[英]passing PHP variable to sql query in oci_parse

I am passing a PHP varibale into a oracle sql query. 我将PHP变量传递到oracle sql查询中。 but its not taking it properly giving me ORA errors like - invalid character. 但它不能正确地给我ORA错误,例如-无效字符。 I tried escaping the varibale as \\'$sid\\', this makes error go, but the query doesnt return anything. 我尝试将变量转义为\\'$ sid \\',这使错误消失了,但是查询不返回任何内容。 Is there a way to pass PHP variable to oracle query 有没有办法将PHP变量传递给oracle查询

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. 为了简洁起见,我已删除了数据库连接部分。

'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. 'SELECT emp from table emp=\\'$sid\\''是一个完全传递给Oracle的字符串,这就是为什么它不起作用的原因。

You need to use oci_bind_by_name to bind a placeholder to a PHP variable. 您需要使用oci_bind_by_name将占位符绑定到PHP变量。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM