lookee Posted October 26, 2008 Share Posted October 26, 2008 I'm attempting to add multiple columns to a pre-existing table. I have tried just about every piece of example code I can find, but I continue getting syntax errors. My Code: // Add multiple columns in the selected database mysql_query("ALTER TABLE nixon( ADD title VARCHAR (30), books VARCHAR (30), publisher VARCHAR (30), ISBN VARCHAR (30), contact VARCHAR (30), PRIMARY KEY(title))") or die(mysql_error()); echo "Table Created!"; include 'close_db.php'; ?> Error Message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( ADD title VARCHAR (30), books VARCHAR (30), publisher VARCHAR (30), ISBN V' at line 1 Any help would be greatly appreciated! : ) Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/ Share on other sites More sharing options...
Barand Posted October 26, 2008 Share Posted October 26, 2008 syntax ALTER TABLE `data` ADD COLUMN `col1` VARCHAR(45) , ADD COLUMN `col2` INTEGER UNSIGNED , ADD COLUMN `col3` DATETIME ; Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675147 Share on other sites More sharing options...
lookee Posted October 26, 2008 Author Share Posted October 26, 2008 Thanks, but I changed the code to... (In case it makes a difference, my Mysql version is Ver. 5.0) Code: ------------- // Create a MySQL table in the selected database mysql_query("ALTER TABLE nixon( ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30), PRIMARY KEY(title))") or die(mysql_error()); echo "Table Created!"; include 'close_db.php'; ?> Error Message... You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN pu' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675171 Share on other sites More sharing options...
corbin Posted October 26, 2008 Share Posted October 26, 2008 No ()s needed. Also, the primary key adding will probably need to be a separate command. Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675187 Share on other sites More sharing options...
lookee Posted October 26, 2008 Author Share Posted October 26, 2008 Thanks, but when I removed the () // Create a MySQL table in the selected database mysql_query("ALTER TABLE jos_divvy( ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30),") or die(mysql_error); echo "Table Created!"; include 'close_db.php'; ?> -------------------- Error Code: mysql_error Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675210 Share on other sites More sharing options...
Barand Posted October 26, 2008 Share Posted October 26, 2008 1 ) it's or die(mysql_error()); 2 ) the (...) after the tablename are still there Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675231 Share on other sites More sharing options...
lookee Posted October 27, 2008 Author Share Posted October 27, 2008 What do you mean (...) ? There is no ... after the table name? Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675327 Share on other sites More sharing options...
corbin Posted October 27, 2008 Share Posted October 27, 2008 *screams* We mean the parenthesis. Parenthesis are used in a CREATE TABLE blah () command, but not in an alter table command. http://dev.mysql.com/doc/refman/5.0/en/alter-table.html Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675335 Share on other sites More sharing options...
lookee Posted October 27, 2008 Author Share Posted October 27, 2008 *sighs* I removed the ( ) from around the alter table. Nothing is working. Everyone thinks they're saying what they mean, but they aren't. For example, the syntax for the Alter table one the Mysql site doesn't have any ( ), but I also noticed it doesn't have and " " around it either... but I am finding tutorials with both both. Nothing is working. I can get one line to work all day long. I've cut and pasted code from other tutorials and built a sample database around the silly code... still nothing... // Create a MySQL table in the selected database mysql_query "ALTER TABLE jos_divvy ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30);" or die(mysql_error()); echo "Table Created!"; include 'close_db.php'; ?> Error Code Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/silliso2/public_html/divvy/add columns.php on line 24 Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675355 Share on other sites More sharing options...
corbin Posted October 27, 2008 Share Posted October 27, 2008 Have you ever heard of a function? I'm going to assume you haven't. Think of a math function. f(x) = 2x for example f of 1 = 2 f of 2 = 4 f of 3 = 6 So on.... That can also be represented by f(1) = 2, f(2) = 4, f(3) = 6. Similarly, mysql_query is a function. In PHP, the basic calling syntax of a function is func([param1, param2, param3, ....]) Or in more simple terms: func($parameter) Where there can be any number of parameters.... func($oneparameter); func($oneparam, $twoparams); func($one, $two, $three); Now, in PHP there are also data types. A data type, as the name implies is a type of data. It can be numeric, strings, or what ever. For example, $x = 5. x would be an integer. $x = 5.5; x would be a double precision floating point number. $x = "hello". x would be a string. $x = 'hello'; x would be a string. So, now that we've gotten that out of the way, " and ' are both string wrappers (for lack of a better term). Anything inside of quotes, or anything inside of single quotes is considered a string. I should mention now that constants can be passed into a function. For example: func(1); or func(1, 2, 3); Or func("hello"); Ok, now, mysql_query is layed out as mysql_query(string $query[, resource $link]) (the link param is optional) Ok, so we want to pass a string to a function. mysql_query("My query here"); So earlier, when we said remove the parenthesis, we meant only the ones inside of the query. mysql_query("ALTER TABLE jos_divvy ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30);"); Learn PHP basics. (P.S. No idea if adding multiple columns is possible with one alter table command.) Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675382 Share on other sites More sharing options...
fenway Posted October 27, 2008 Share Posted October 27, 2008 (P.S. No idea if adding multiple columns is possible with one alter table command.) Yup. Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675851 Share on other sites More sharing options...
lookee Posted October 27, 2008 Author Share Posted October 27, 2008 Thanks for the response. I'm sitting in front of O'Reilly's Learning PHP 5, O'Reilly's Learning PHP & Mysql, and Gosselin's PHP with MySQL, and not one of them show an example of this, so I'm trying to learn the basics ... but.... even as you explain it all and it sounds clear and everything... which is great... and I am brushing up on the basics... it still doesn't work. Which is the problem... It's not the concepts that don't make sense... it's actually plugging the code in and actually having it work that is the problem. I don't mean to start some sort of insane thread ... people seem to be very helpful, but plain and simple... it "still does not work." ------------------------------------------------- // Create a MySQL table in the selected database mysql_query("ALTER TABLE jos_divvy ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30);"); or die(mysql_error()); <---- line 26 echo "Table Created!"; include 'close_db.php'; ?> Error Message Parse error: syntax error, unexpected ';' in /home/silliso2/public_html/divvy/add columns.php on line 26 --------------------------------- Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675897 Share on other sites More sharing options...
Barand Posted October 27, 2008 Share Posted October 27, 2008 mysql_query("ALTER TABLE jos_divvy ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30)") or die(mysql_error()); echo "Table Created!"; include 'close_db.php'; Quote Link to comment https://forums.phpfreaks.com/topic/130185-adding-multiple-columns-to-pre-existing-table/#findComment-675910 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.