473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Variable as tablename in query

I have a query that looks like this:

mysql_query("UPDATE layer1108 SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");

And it works the way I want, but I want the layer1108 to be dynamic. I have tried declaring this ahead of time and then putting that variable in. This doesn't work:

mysql_query("UPDATE $layerID SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");

But when I do this it works:

$dynamic_table_name = "layout1108";
mysql_query("UPDATE $dynamic_table_name SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");

But it doesn't work when I again make the "layout1108" a $layoutID variable.

Any thoughts on how to get this to work?
Jun 21 '07 #1
2 1830
r035198x
13,262 8TB
I have a query that looks like this:

mysql_query("UPDATE layer1108 SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");

And it works the way I want, but I want the layer1108 to be dynamic. I have tried declaring this ahead of time and then putting that variable in. This doesn't work:

mysql_query("UPDATE $layerID SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");

But when I do this it works:

$dynamic_table_name = "layout1108";
mysql_query("UPDATE $dynamic_table_name SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");

But it doesn't work when I again make the "layout1108" a $layoutID variable.

Any thoughts on how to get this to work?
This looks like a problem in your PHP code. The only difference between when you use $dynamic_table_name and when you use $layerID is the variable name, now since both are valid PHP identifiers, the problem must be with the value that you are setting when you use $layerID. How are you assigning values to it and what is the error that you're getting when you run it?
Jun 21 '07 #2
pbmods
5,821 Expert 4TB
Heya, inaz4sun.

But it doesn't work when I again make the "layout1108" a $layoutID variable.
What do you mean by 'it doesn't work'? Try echoing the output of mysql_error().

Also, it would probably be a good idea to enclose your table name in backticks, just so that if something is wrong with the value of $dynamic_table_name, your queries will just fail instead of (potentially) doing something destructive:

Expand|Select|Wrap|Line Numbers
  1. $dynamic_table_name = "layout1108";
  2. mysql_query("UPDATE `$dynamic_table_name` SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");
  3.  
Jun 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rudi | last post by:
Hello Forum ! I want to have the tablename "dbo.Enbxxxx" as an additional parameter for a procedure like this: ALTER Procedure prcSucheUNR ( @UNR int ) As
4
by: dixie | last post by:
I want to write a query where one of the two tables in it is variable and its name is picked up from a text box control on an open form. The query is very simple and is only a select query with...
1
by: Kruno Milicevic | last post by:
I want to fill DataGrid with some table from my database. I usual use something like this: string sqlString ="SELECT * FROM TableName"; OleDbCommand myOleDbCommand = new OleDbCommand(sqlString ,...
3
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
4
by: Ying Lu | last post by:
Hello, Under mysql, we have "desc tablename" to get the detail information about a table. My question is about to get column name, and column type for a specific table under PostgreSQL through...
1
by: traceable1 | last post by:
SQL Server 2000 SP4 with AWE hotfix. Windows 2003 SP1. I have a stored procedure which is not working the way I think it should be. I have a CURSOR which has a variable in the WHERE clause: ...
3
by: JF | last post by:
I would like to automate a Union query going through the tables of my dataset using VBA: I have this code: ------------------------------------------------------------------- Sub ssql() Dim...
9
by: Slickuser | last post by:
Hi, I'm trying to use my variable in another variable. How can I achieve that? I can use + (concat) but it will be a lot me to concatenate. I have to use 3 variables with 15 replacement. ...
3
by: dmorand | last post by:
I'm very new to stored procedures so this is probably something very dumb. I want to pass a name of a table to be created to my stored procedure. I'm using the variable @tableName in the code below...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.