Gw Temp


Tutorial - 'The Basics of SQL' by rpgsmo

An item about Programming Languages posted on


This covers the basics of the Structured Query Language (SQL), basically the most common queries such as SELECT, INSERT, UPDATE and DELETE. Check it out.


INSERT Function

Inserting into a table in mysql is fairly simple, first off, you use the mysql_query().


Then, you have to type INSERT INTO in the parenthesis in quotes, this is pretty self explanatory.

mysql_query("INSERT INTO");

After that, you should type the table name, and some other stuff, I will show you:

mysql_query("INSERT INTO tblname () VALUES ()");

The first set of parenthases after "tblname" you insert the columns:

mysql_query("INSERT INTO tblname (age) VALUES ()");

Next, you insert the values after "VALUES", like so:

mysql_query("INSERT INTO tblname (age) VALUES (21)");

And that's all.

SELECT Function

Ok, this tutorial focuses on selecting from the table that we used in the last tutorial, and printing it out for people to see. First off, we type the usual:


Then, in between the parentheses we type the SELECT * FROM query:

mysql_query("SELECT * FROM");

The * inbetween the SELECT and FROM means to select every column in the table, to only select one, we simply type:

mysql_query("SELECT columname FROM");

In our case, "columname" will be "age", now for selecting from the table:

mysql_query("SELECT age FROM tblname");

Now, we have to type an identifier for it;

$query = mysql_query("SELECT age FROM tblname");

Now we simply gather all the information from the table using a simple while() loop and display it:

while ($data=mysql_fetch_assoc($query)) {
print "Age : $data[age]";

Pretty self explanatory.

UPDATE Function
First off, the UPDATE function is simply used to update a row in the mysql table, it is very very simpe.

To update a record in the database, type:


Between the quotation marks, type UPDATE followed by the tablename. Then simply type SET:

mysql_query("UPDATE tblname SET");

Then type the name of the field you want to update with `'s surrounding it, followed by an equal sign, then type the new value in apostraphe's:

mysql_query("UPDATE tblname SET `age` ='24'");

If you would like the change other fields, simply repeat the above step with a comma followed by each field:

mysql_query("UPDATE tblname SET `age` = '24', `name` = 'Bob'");

Almost done, next you have to specify where you want it all to update, this is very very simple:

mysql_query("UPDATE tblname SET `age` = '24', `name` = 'Bob' WHERE `id` = '1'");

Easy eh? Now we will learn if the function works out right by typing some more stuff, like so:

$query = mysql_query("UPDATE tblname SET `age` = '24', `name` = 'Bob' WHERE `id` = '1'")or print mysql_error();

if ($query == TRUE) {
print "The record was updated successfully.";
} else {
print "There was an error in the record, please look up to see what the error is.";

Simple eh? Ok, that's it for the UPDATE function.

DELETE Function
This is the part of the tutorial for deleting a row, it's quite simple, it works like update, first type:


Then, between the parenthases type DELETE FROM followed by the table in `'s:

mysql_query("DELETE FROM `tblname`");

Then, simply type where you want to delete it from, like so:

mysql_query("DELETE FROM `tblname` WHERE id = '1'");

And guess what? That's it! That's all for my basics of sql tutorial, now go have some fun and make a forum!

Until next time, c ya, and remember to visit my site at http://bullzeyeproduction please, thanks.