在 sql 语句中使用 php 变量进行串联

Use php variable in sql statement with concatenation

我下面有一个变量 (storecode),它从用户的输入中获取它的值 (120)

$storecode = $_POST['storecode'];

在 PL/SQL 开发人员中工作的 sql 是 Select mydate from tbl_store@rm_database.p120081;

我试图用 $storecode 变量替换“120”,然后连接“081”,所以我有:

 " SELECT mydate from tbl_store@rm_database.p'$storecode'" "081";

显然以上是错误的,需要一些指导来解决这个问题

您可以创建一个新变量 table_name 并将串联的 databasename.tablename 添加到该变量,例如

var table_name = 'tbl_store@rm_database.p'.$storecode.'081'

然后在您的查询中,您可以将此变量用作

"SELECT mydate from ".$table_name;

我不是 php 开发人员,这只是出于想法,我根据我对 php 的基本知识分享代码,因此您需要解决语法错误。

您的表达式中的单引号被添加到表名中,因此,假设 $storecode = 120,此语句:

 "SELECT mydate from tbl_store@rm_database.p'$storecode'081";

... 导致 tbl_store@rm_database.p'120'081

的文字表名

您可以按照其他答案中的建议进行连接;但是,您可以在了解 variable parsing 的字符串定义中获得相同的结果(在“变量解析”部分下查找“简单语法”)。

 "SELECT mydate from tbl_store@rm_database.p{$storecode}081";

If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.

没有大括号,解析器会尝试引用变量 $storecode081

所以你最初是在正确的行上,你只需要大括号语法而不是单引号。