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):
$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!
You dont need " after constant.
"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!