简体   繁体   English

PHP PDO:SQL查询结束时的常量变量

[英]PHP PDO: Constant Variable at End of SQL Query

I can't seem to figure this one out, I'm trying to globalize all of the database credentials across my application , therefore I'm setting the database table name in a global constants file. 我似乎无法弄清楚这一点,我试图在我的应用程序中全球化所有数据库凭证,因此我在全局常量文件中设置数据库表名称。 I have one query that no matter how many ways I try to set the constant variable in the string, it fails miserably. 我有一个查询,无论我尝试用多少种方法在字符串中设置常量,它都会失败。 Anyone have any ideas? 有人有想法么?

Here's the query before the constant variable change, which works: 这是常量变量更改之前的查询,该查询有效:

$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM crmManager");

Here's a few different attempts that all fail (the constant variable is TBL_NAVIGATION): 这是几次失败的尝试,所有尝试都失败了(常量变量为TBL_NAVIGATION):

$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM " . TBL_NAVIGATION . ");    
$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM".TBL_NAVIGATION.");
$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM".TBL_NAVIGATION);
$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM {TBL_NAVIGATION}");
$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM".TBL_NAVIGATION");

I even tried to set it initially as a variable: 我什至尝试将其初始设置为变量:

$table = TBL_NAVIGATION;
$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM {table}"); 
 // I've tried every possible way to include the variable

How can I fix this? 我怎样才能解决这个问题? Thanks! 谢谢!

常量后,您不需要“”。

"SELECT MAX(salesPosition) FROM " . TBL_NAVIGATION 

Always check your syntax is the lesson to be learned here: 始终检查您的语法是要在这里学习的课程:

The correct query was: 正确的查询是:

$stmt = $dbh->prepare("SELECT MAX(salesPosition) FROM ".TBL_NAVIGATION);

Thanks to Kaii for waking me up! 感谢Kaii唤醒我!

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

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