-BloodStaind- Posted August 8, 2008 Share Posted August 8, 2008 I'm trying to create mySQL tables in PHP, but I can't seem to get the code to work. I've been learning PHP for about 2 days now, so if the code is way off... My bad. Here it is: <?php $con = mysql_connect("localhost","username","password"); mysql_select_db("dbname"); $sql = "SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; DROP TABLE IF EXISTS mqs_users; CREATE TABLE IF NOT EXISTS mqs_users ( id int(11) NOT NULL auto_increment, username varchar(225) NOT NULL, password varchar(225) NOT NULL, email varchar(225) NOT NULL, UNIQUE KEY id (id) )"; if (!$con) { echo('Could not connect: ' . mysql_error()); exit; } if(mysql_query($sql)) { echo "Table succesfull"; } mysql_query($sql, $con); ?> I need to know what I'm doing wrong. Link to comment https://forums.phpfreaks.com/topic/118808-mysql-tables-in-php/ Share on other sites More sharing options...
obsidian Posted August 8, 2008 Share Posted August 8, 2008 In PHP, you cannot send multiple queries to a single mysql_query() call. You would have to actually send each query individually. Other DBs, such as Postgresql, do allow this behavior, but the MySql connections in PHP do not. Link to comment https://forums.phpfreaks.com/topic/118808-mysql-tables-in-php/#findComment-611758 Share on other sites More sharing options...
-BloodStaind- Posted August 8, 2008 Author Share Posted August 8, 2008 Alright, I changed "mysql_query($sql, $con);" to "mysql_query($sql); And it still doesn't work. Link to comment https://forums.phpfreaks.com/topic/118808-mysql-tables-in-php/#findComment-611764 Share on other sites More sharing options...
obsidian Posted August 8, 2008 Share Posted August 8, 2008 Alright, I changed "mysql_query($sql, $con);" to "mysql_query($sql); And it still doesn't work. No, what I'm saying is that you cannot execute multiple SQL statements in a single query: <?php $sql = <<<EOQ DROP TABLE IF EXISTS mytable; CREATE TABLE mytable (...); EOQ; mysql_query($sql); ?> That does not work. You have to query each individually: <?php mysql_query("DROP TABLE..."); mysql_query("CREATE TABLE..."); ?> Link to comment https://forums.phpfreaks.com/topic/118808-mysql-tables-in-php/#findComment-611767 Share on other sites More sharing options...
-BloodStaind- Posted August 8, 2008 Author Share Posted August 8, 2008 Alright, I changed "mysql_query($sql, $con);" to "mysql_query($sql); And it still doesn't work. No, what I'm saying is that you cannot execute multiple SQL statements in a single query: <?php $sql = <<<EOQ DROP TABLE IF EXISTS mytable; CREATE TABLE mytable (...); EOQ; mysql_query($sql); ?> That does not work. You have to query each individually: <?php mysql_query("DROP TABLE..."); mysql_query("CREATE TABLE..."); ?> Oh, hehe oops. Thanks man! Link to comment https://forums.phpfreaks.com/topic/118808-mysql-tables-in-php/#findComment-611768 Share on other sites More sharing options...
obsidian Posted August 8, 2008 Share Posted August 8, 2008 Oh, hehe oops. Thanks man! Just another thought... you could also just split up your queries with a loop, too: <?php $queries = explode(';', $sql); foreach($queries as $q) { $q = trim($q); mysql_query($q); } ?> This way, you can still set up your create statements in one file or variable and parse it out separately. Be careful, though, because this rudimentary illustration will break if you are inserting any values that contain semicolons. Link to comment https://forums.phpfreaks.com/topic/118808-mysql-tables-in-php/#findComment-611772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.