Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add column to a table

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
Hi everyone,
I tried to add a column to an existing table using the following code:
Code:
 <?php
$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:
I wish someone explained me that error message[sad].
Thanks
 
Hi

You can not have MySQL code thrown like that in the middle of a PHP script.
PHP:
<?php
$dbcon = @mysqli_connect([green][i]'localhost'[/i][/green], [green][i]'user'[/i][/green], [green][i]'pass'[/i][/green], [green][i]'db'[/i][/green]) [b]OR[/b] die([green][i]'Could not connect to MySQL: '[/i][/green] . mysqli_connect_error()); 
mysqli_set_charset($dbcon, [green][i]'utf8'[/i][/green]);
mysqli_query($dbcon, [green][i]'ALTER TABLE users ADD phone VARCHAR(11)'[/i][/green]);
?>

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

Feherke.
feherke.ga
 
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
 
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.
 
Thanks,
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..
Thanks
 
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...
 
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.
 
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.

 
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.
 
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.
SQLyog_wine.png
 
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.
Thanks
 
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.
 
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.
 
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.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
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..
 
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.
 
I'll take your advise and give it a try ("never understood copy/paste concepts" thats my name[bigsmile]...).
Thanks a lot !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top