If I use the following
define('MENUS','menuitems');
and then later try
$sql ="SELECT * FROM MENUS where Menu = '$menuname'";
I keep getting the message that the table ....MENUS does not exist. It does not matter what I put after the FROM, it takes it literally and does not use the pre defined define.
Is there something wrong with the declaration or using the single quotes (I have tried all variations).
Define error ?
Moderator: General Moderators
Re: Define error ?
It's not an error. You should use concatenation to create query
otherwise all your messages containing 'MENUS' would change to 'menuitems'...
Code: Select all
$sql = 'SELECT * FROM ' . MENUS . ' where Menu = "$menuname"';Re: Define error ?
That is the idea. I want to define the name of the table hence define ('MENUS', 'menuitems'), but it does not replace the word MENUS with menuitems when I use select * from MENUS, it just gives me an error that table MENUS does not exist. This I know already. It should have been replaced with menuitemes.
Other defines I have used work fine with code like
$connection = @mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error());
I just cannot see why the code
$sql ="SELECT * FROM MENUS where Menu = '$menuname'"; fails. I have to use
$sql ="SELECT * FROM menuitems where Menu = '$menuname'"; and that works fine.
Other defines I have used work fine with code like
$connection = @mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error());
I just cannot see why the code
$sql ="SELECT * FROM MENUS where Menu = '$menuname'"; fails. I have to use
$sql ="SELECT * FROM menuitems where Menu = '$menuname'"; and that works fine.
Re: Define error ?
konrad gave you the answer. In PHP a constant isn't expanded within single quotes. You need to concatenate.