Jump to content

adding multiple columns to pre-existing table


Recommended Posts

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!

: )

 

 

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

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

*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

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.)

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

 

---------------------------------

 

 

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';

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.