943,908 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1961
  • PHP RSS
Jun 30th, 2008
0

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

Expand Post »
Hey,

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

PHP Syntax (Toggle Plain Text)
  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 6:20 pm.
Similar Threads
Reputation Points: 32
Solved Threads: 3
Posting Whiz in Training
MaxMumford is offline Offline
228 posts
since Oct 2006
Jun 30th, 2008
0

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

you can do that.

try this:

PHP Syntax (Toggle Plain Text)
  1. $tablename = $_POST['tablename'];
  2. $sql = "CREATE TABLE `databasename`.`{$tablename}` (//table fields here)";
  3. mysql_query($sql);
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jun 30th, 2008
0

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

php Syntax (Toggle Plain Text)
  1. $tablename = "test";
  2. $query = "create table ".$tablename." (name varchar(200) not null )";
  3. mysql_query($query);
Like this.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 1st, 2008
0

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

Thanks for the help however I don't see how this is not working :s
PHP Syntax (Toggle Plain Text)
  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)
Reputation Points: 32
Solved Threads: 3
Posting Whiz in Training
MaxMumford is offline Offline
228 posts
since Oct 2006
Jul 1st, 2008
0

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

Missing ending )
P.S. group is a keyword. Its not advisable to use keywords as column names.
This works fine for me.
php Syntax (Toggle Plain Text)
  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))";
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 1st, 2008
0

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

PHP Syntax (Toggle Plain Text)
  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. );

Click to Expand / Collapse  Quote originally posted by MaxMumford ...
Hey,

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

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 16
Solved Threads: 9
Junior Poster
mexabet is offline Offline
148 posts
since Mar 2008
Jul 1st, 2008
0

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

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
Reputation Points: 32
Solved Threads: 3
Posting Whiz in Training
MaxMumford is offline Offline
228 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: include function in php
Next Thread in PHP Forum Timeline: get sets of info from db depending on hidden field's value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC