Jump to content

Creating a table in Database with php not working


justin7410
 Share

Go to solution Solved by boompa,

Recommended Posts

$create_table = "SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `" .$date."`;
CREATE TABLE `".$date."` (
  `transaction_id` varchar(11) NOT NULL,
  `name` varchar(255) character set latin1 default NULL,
  `price` varchar(255) character set latin1 default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8";

$run_query = mysql_query($create_table);

SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `Mar_15_2014`; CREATE TABLE `Mar_15_2014` ( `transaction_id` varchar(11) NOT NULL, `name` varchar(255) character set latin1 default NULL, `price` varchar(255) character set latin1 default NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8

 

 

Ok so i am some what confused as to why my table is not being created with this.

 

When I insert the query directly into SQL the table is created with no error or issue. So i know the statement is true and syntax is not an issue.

 

When i create the variable in PHP with the same query, then try to run the query, the same table is then not created in the DB.

 

Am i missing a step ? usually this is pretty straight forward when working with DB. I would usually grab the data then fetch the array, then extract the data into a set of variables.

 

Any suggestions ?

Link to comment
Share on other sites

you cannot string multiple queries together and run them at one time. you must run each query separately -

 

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

 

 

next, you should NOT be making a database table using a date as the table name. that is a bad design that will prevent you from easily using the data spread out across the many tables. you should have a date column in ONE table that identifies the date the data corresponds to.

Link to comment
Share on other sites

  • Solution

Yeah, that's a pretty broken schema design. First thing you need to do is revisit that, perhaps find a good relational database tutorial.

 

I see people trying to use PHP to create and manipulate database schemas all the time, and unless you're using some sort of migration facility from a framework, it seems a bit of an exercise in futility.

 

I have always written straight SQL scripts and fed them into the mysql command line client through stdin.

 

transactions.sql

DROP TABLE IF EXISTS `transactions`;
 
CREATE TABLE `transactions` (
  `transaction_id` varchar(11) NOT NULL,
  `name` varchar(255) character set latin1 default NULL,
  `price` varchar(255) character set latin1 default NULL,
  `transaction_date` DATETIME default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
$ mysql --database=mydatabase < transactions.sql

Also, unless things have changed, MyISAM tables do not support foreign keys so using SET FOREIGN_KEY_CHECKS is useless.

Edited by boompa
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.