Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Add column to a table

Add column to a table

Add column to a table

Hi everyone,
I tried to add a column to an existing table using the following code:


$dbcon = @mysqli_connect ('localhost', 'user', 'pass', 'db') OR die ('Could not connect to MySQL: ' . mysqli_connect_error () ); 
mysqli_set_charset($dbcon, 'utf8');
ALTER TABLE users ADD phone VARCHAR(11);
But I get the following error:

Quote (Parse error: syntax error, unexpected 'TABLE' (T_STRING) in C:\xampp\htdocs\simpleIdb\add_field.php on line 4)

I wish someone explained me that error messagesad.

RE: Add column to a table


You can not have MySQL code thrown like that in the middle of a PHP script.


$dbcon = @mysqli_connect('localhost', 'user', 'pass', 'db') OR die('Could not connect to MySQL: ' . mysqli_connect_error()); 
mysqli_set_charset($dbcon, 'utf8');
mysqli_query($dbcon, 'ALTER TABLE users ADD phone VARCHAR(11)');

By the way, this is not a MySQL issue, but a PHP one.


RE: Add column to a table

To run MYSQL statements you need to use the appropriate function and pass the sql statement to it as a string.

The error is telling you that your sql statement is unexpected like that there.

Phil AKA Vacunita
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Add column to a table

OK, got it.

RE: Add column to a table

One more thought...

Unless you are writing an upgrade function for an existing web application, you probably should not be doing this in PHP. A direct command to the SQL server would be fine for this one-time operation. Repeated execution of this PHP script will result in MySQL errors, which you'll either need to catch or ignore.

RE: Add column to a table

I only wanted to add a column to a table. I guess this is a "one-time operation".
Either way I'll have to refresh my knowledge of MySQL commands..

RE: Add column to a table

Thanks spamjim.
I think I'd rather read that handbook of MySQL. Inernet programming is so diverse (html, css, js, php, photoshop etc..) I wouldn't like to spread it to more utilities. I'm confused enough as it is now...

RE: Add column to a table

photoshop? internet programming?

mysql is not a utility - it is a relational database server that comes with a command line client and some interface libraries.

RE: Add column to a table

jpadie, I think lupidol is fearful of MySQL Workbench, not MySQL. But this fear makes no sense. A MySQL GUI lets you manually type SQL or use the graphic interface to send commands to the MySQL server. And even in the latter, the Workbench returns the query so you can learn from it.

You would be shooting yourself in the foot by not using an essential tool like Workbench.

There is a reason why we call these utilities. Manual coding is sooooo last century.

When I started in computer graphics, I had to write code to render each pixel in a 1-bit graphic. I can't imagine the mentality that Photoshop is more confusing than creating graphics only in code.

RE: Add column to a table

ah. I see.
I started using workbench recently but quickly abandoned it. I was wanting to see, for a client, whether it would allow visual query modelling and automatic join creation like access does. Couldn't make it work.

so i built the client a VM with access in it that attached to the (remote) mysql database over odbc. problem solved.

i doubt whether he can manipulate the data structure that way (well ... i really hope that he can't) but that's not the goal.

fwiw, i use the command line mostly for database admin. and when I'm trying loads of different queries to test for optimisation (or just because i can't get the joins right in my head to get the data out) I use php (for when I want to re-edit the queries bit by bit) or phpmyadmin (for when i can't remember the right order to put the brackets in for index creation etc). i don't use the command line for long queries because i can't cut and paste easily from an editor. i think it's something to do with the autocomplete.

RE: Add column to a table

I don't actually use Workbench. I use SQLyog Community Edition (it runs on Windows and Linux, with Wine). In addition to phpmyadmin, there's also HeidiSQL. They are essentially all the same. You can craft a query and see the result set in the panel below. You can even modify a value in the result set without crafting an update query in the query editor.

But the kicker with these GUI tools is that you can run multiple query tabs and multiple database tabs. I could not imagine maintaining my sanity doing this from a command line.

This image shows the commercial edition but the screen is showing a basic function available even to the free community edition.

RE: Add column to a table

Thanks for enlightening more possibilities and utilities.
phpMyAdmin is as far as i can go with utilities.
I started web programming with "Dreamweaver" which was fine at the beginning but when i bumped into problems and looked for books on Dreamweaver (well, that was long time ago..) I noticed that DW books were ten times thicker then HTML JS PHP and MySQL together... So I'l stick to coding and bother with reading notes I wrote to myself some time ago when I worked with SQL.

RE: Add column to a table

IMO dreamweaver overcomplicates for less results. Some of the code I have seen come out of DW 'modules' is dangerous (from a security perspective) and teaches really bad practices. I'd hope that the books are thicker because they teach you how to avoid these. But I doubt it.

my advice is to write stuff in an IDE or a text editor. learn the hard way. you'll find that when you get one language 'mastered' the others become obvious bar their syntax. and the syntax is easy to get from reference books.

RE: Add column to a table

Best IDE for me is Notepad++ and I tried many other utilities (NUSPHERE?) for PHP.
At least it saves me tagging where an element or command starts and where it ends plus it doesn't insert code which I cannot understand or cannot change later.

RE: Add column to a table

I'll throw a vote in for HeidiSQL if you want something to build and test queries in a GUI/IDE and see the SQL command string that it creates.


Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Add column to a table

Maybe I'll try it.
One more thing I remember now, when Dreamweaver or Nuspere were opened, there were other packages I was unable to upload, either Photoshop or SQLServer, I dont remember.
I'm kind of prejudice concerning utilities..

RE: Add column to a table

I started out with HeidiSQL when it was known as MySQL-Front. It has come a long way. Years ago I put the program in front of a coworker with absolutely no computer skills (she never understood copy/paste concepts) yet she was able to handle it well for minor data updates and selects. So there really is nothing to fear from using a helpful tool.

RE: Add column to a table

I'll take your advise and give it a try ("never understood copy/paste concepts" thats my namebigsmile...).
Thanks a lot !

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close