using a variable to name the table in the CREATE TABLE function

Please support our PHP advertiser: 50% off 6 Months Dedicated Server Hosting from 1&1!
Thread Solved

Join Date: Oct 2006
Posts: 215
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 2
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Posting Whiz in Training

using a variable to name the table in the CREATE TABLE function

 
0
  #1
Jun 30th, 2008
Hey,

Im just wondering how i can get something like this to work:

  1. $tablename = $_POST['tablename'];
  2. mysql_query(CREATE TABLE `databasename`.`$tablename` (`name` VARCHAR( 200 ) NOT NULL))

At the moment its coming up with an error because I am using a variable for the table name instead of inputting it normally.

Thanks,

Max
Last edited by MaxMumford; Jun 30th, 2008 at 5:20 pm.
| IPCopy |
The only google chrome extension that lets you copy your IP to the clipboard with one click.
https://chrome.google.com/extensions...ogchjmmgjjfola
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,234
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 169
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: using a variable to name the table in the CREATE TABLE function

 
0
  #2
Jun 30th, 2008
you can do that.

try this:

  1. $tablename = $_POST['tablename'];
  2. $sql = "CREATE TABLE `databasename`.`{$tablename}` (//table fields here)";
  3. mysql_query($sql);
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,833
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 344
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: using a variable to name the table in the CREATE TABLE function

 
0
  #3
Jun 30th, 2008
  1. $tablename = "test";
  2. $query = "create table ".$tablename." (name varchar(200) not null )";
  3. mysql_query($query);
Like this.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 215
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 2
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Posting Whiz in Training

Re: using a variable to name the table in the CREATE TABLE function

 
0
  #4
Jul 1st, 2008
Thanks for the help however I don't see how this is not working :s
  1. $sql = 'CREATE TABLE `web163-gallery-7`.`{$picurl}` (`name` VARCHAR( 200 ) NOT NULL ,`picurl` VARCHAR( 200 ) NOT NULL ,`group` VARCHAR( 200 ) NOT NULL ,`dateadded` TIMESTAMP( 200 ) NOT NULL DEFAULT CURRENT_TIMESTAMP ,`id` INT( 200 ) NOT NULL ,PRIMARY KEY ( `id` ) ,UNIQUE (`name` ,`picurl`)';
  2.  
  3. mysql_query($sql)
| IPCopy |
The only google chrome extension that lets you copy your IP to the clipboard with one click.
https://chrome.google.com/extensions...ogchjmmgjjfola
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,833
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 344
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: using a variable to name the table in the CREATE TABLE function

 
0
  #5
Jul 1st, 2008
Missing ending )
P.S. group is a keyword. Its not advisable to use keywords as column names.
This works fine for me.
  1. $sql = "CREATE TABLE test.".$picurl ."
  2. (name VARCHAR(200) NOT NULL ,
  3. picurl VARCHAR( 200 ) NOT NULL ,
  4. group1 VARCHAR( 200 ) NOT NULL ,
  5. dateadded TIMESTAMP( 200 ) NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  6. id INT( 200 ) NOT NULL ,
  7. PRIMARY KEY ( id ) ,UNIQUE (name ,picurl))";
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 142
Reputation: mexabet is an unknown quantity at this point 
Solved Threads: 9
mexabet's Avatar
mexabet mexabet is offline Offline
Junior Poster

Re: using a variable to name the table in the CREATE TABLE function

 
0
  #6
Jul 1st, 2008
  1. # Drop any existing table `posts`
  2. # --------------------------------------------------------
  3. DROP TABLE IF EXISTS `posts`;
  4.  
  5. # --------------------------------------------------------
  6. # Table structure for table `posts`
  7. # --------------------------------------------------------
  8. CREATE TABLE posts (
  9. post_id int(11) NOT NULL auto_increment,
  10. category_id int(11) NOT NULL,
  11. user_id int(11) NOT NULL,
  12. title varchar(150) NOT NULL,
  13. body text NOT NULL,
  14. posted timestamp,
  15. PRIMARY KEY (post_id)
  16. );

Originally Posted by MaxMumford View Post
Hey,

Im just wondering how i can get something like this to work:

  1. $tablename = $_POST['tablename'];
  2. mysql_query(CREATE TABLE `databasename`.`$tablename` (`name` VARCHAR( 200 ) NOT NULL))

At the moment its coming up with an error because I am using a variable for the table name instead of inputting it normally.

Thanks,

Max
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 215
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 2
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Posting Whiz in Training

Re: using a variable to name the table in the CREATE TABLE function

 
0
  #7
Jul 1st, 2008
Thanks I thought it wasnt working but that was because i needed to call the table something with .jpg at the end and iv realised i cant do that. No worries iv found a work around.

Max
| IPCopy |
The only google chrome extension that lets you copy your IP to the clipboard with one click.
https://chrome.google.com/extensions...ogchjmmgjjfola
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1317 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC