Define error ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jjl
Forum Newbie
Posts: 13
Joined: Wed May 14, 2008 12:31 am

Define error ?

Post by jjl »

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).
konrad
Forum Newbie
Posts: 3
Joined: Tue May 20, 2008 4:10 pm

Re: Define error ?

Post by konrad »

It's not an error. You should use concatenation to create query

Code: Select all

$sql = 'SELECT * FROM ' . MENUS . ' where Menu = "$menuname"';
otherwise all your messages containing 'MENUS' would change to 'menuitems'...
jjl
Forum Newbie
Posts: 13
Joined: Wed May 14, 2008 12:31 am

Re: Define error ?

Post by jjl »

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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Define error ?

Post by califdon »

konrad gave you the answer. In PHP a constant isn't expanded within single quotes. You need to concatenate.
Post Reply