View Full Version : INSERT using () or not?

08-18-2008, 06:07 AM
I was looking through some of the tutorials on here and came across a question. In comparing those to a few books and other sites i'm reading, when using the INSERT statement I see two ways, one using the () around the INSERT and one that doesn't.

a. mysql_query("INSERT INTO person (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");


b. $sql="INSERT INTO person (FirstName, LastName, Age)
Does it matter which way you do it? Is the first one for directly insertingand the second one for use through a form?I have seen it both ways is why its confusing.Thanks!

08-18-2008, 07:46 AM
in your examples...

b. This is just a string assigned to a variable


$sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

what you would have to do after assigning the SQL to a string is this...


this is the same as doing...
mysql_query("INSERT INTO person (FirstName, LastName, Age)VALUES('Peter', 'Griffin', '35')");

08-18-2008, 02:58 PM
Thats kind of what i was thinking, but I have also seen it written like this:

$sql=("INSERT INTO person (firstname, lastname, age) VALUES etc...

In other words, I have seen the '(' before the INSERT on a variable statement. Is that wrong?, or could it work that way?

Thanks, as always!!

08-18-2008, 03:00 PM
i reckon thats invalid

never seen that or done it myself that way before

08-18-2008, 03:05 PM
ok, i would rather learn the correct way!

one thing ive realized quickly about php, is there are a million different ways to write one task.!

08-18-2008, 03:14 PM

and when working professionally you have to pickup other programmers work which will be in a totally different style

its a nightmare which is why i always encourage the use of comments and indentation