justin7410 Posted March 15, 2014 Share Posted March 15, 2014 $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 https://forums.phpfreaks.com/topic/286996-creating-a-table-in-database-with-php-not-working/ Share on other sites More sharing options...
mac_gyver Posted March 15, 2014 Share Posted March 15, 2014 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 https://forums.phpfreaks.com/topic/286996-creating-a-table-in-database-with-php-not-working/#findComment-1472725 Share on other sites More sharing options...
boompa Posted March 15, 2014 Share Posted March 15, 2014 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. Link to comment https://forums.phpfreaks.com/topic/286996-creating-a-table-in-database-with-php-not-working/#findComment-1472726 Share on other sites More sharing options...
justin7410 Posted March 15, 2014 Author Share Posted March 15, 2014 Hey guys, Both great answers and very helpful. I am gonna mark boompa as the correct answer , simply for me it was a bit more helpful. Thank guys very much appreciated. Link to comment https://forums.phpfreaks.com/topic/286996-creating-a-table-in-database-with-php-not-working/#findComment-1472729 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.