| MySQL Database Integration |
pages (2): 1 [2] |
|
|
Inserting data
Inserting data is much more simple than selecting it, because you don’t need to
use as many variables. In fact, you just need to use one line of code
(pretty much) and for this we use the mySQL command:
INSERT INTO TableName(Column1,Column2,Column3)
VALUES(‘data1’,’data2’,’data3’)
This may look advanced but its actually extremely simple!
TableName obviously represents the name of the table you want to fill with
the new data. Column1, Column2 and Column3 are just there as
examples, and you can you replace them with the name of the columns in
your table at leisure. Remember that if you don’t include a column, mySQL
just leaves that cell blank on the new entry. The values part
of the above command includes the data. “data1” goes into “Column1”,
“data2” into “Column2”, etc.
So let’s put this into PHP. We are just going to insert one string of data into
our one column, bringing us up to a total of 5 rows.
<?
mysql_connect(“localhost”,”joeblog”,”opensesame”) or die(mysql_error());
mysql_select_db(“joes_database”);
$ourdata = “the data we are going to put into the database”;
mysql_query(“INSERT INTO JoeBlog (Joe) VALUES(‘$ourdata’)”) or
die(mysql_error());
echo ‘If mysql_error() didn’t spit anything at you and you are
reading this message then the insert worked fine!’;
?> |
Now we get on to the easy commands...
Deleting and Editing data
There really isn’t much explaining to do on the subject of these two - they are
just the same as INSERT, except with a different command:
UPDATE TableName SET ColumnName = Value WHERE Row = id
E.g. UPDATE JoeBlog SET Joe = ‘the data we are going to put into the database
but its now edited’ WHERE id = 5
Our table doesn’t have an ID field but we’re going to pretend it does (you can,
in fact, use any field you like). ID fields just give each row a unique ID
number, which are quite often automatically incremented. They sometimes
make database structures a little easier for humans to comprehend, and ensures
that the data in only ONE column is updated by the UPDATE command.
I’ll tie the example in with the DELETE command’s example. Delete has the
format:
DELETE FROM TableName WHERE id = Value
E.g. DELETE FROM JoeBlog WHERE id = 5
The two examples above effectively edit the values we entered into our database
earlier, and then delete it. Here's the combined PHP code for these operations:
<?
mysql_connect(“localhost”,”joeblog”,”opensesame”) or die(mysql_error());
mysql_select_db(“joes_database”);
mysql_query(“UPDATE JoeBlog SET Joe = ‘the data we are going to put
into the database but its now edited’ WHERE id = 5”) or
die(mysql_error());
echo ‘If you are reading this then the data has been successfully
edited. Remember that our id field is imaginary at the moment but
when you make your own table you will include the id field.’;
mysql_query(“DELETE FROM JoeBlog WHERE id = 5”) or die(mysql_error));
echo ‘If you are reading this then the data has been successfully
deleted.’;
?> |
Well, that's it for my introduction to mySQL/PHP. This tutorial was not
aimed to inform you on how to set up mySQL tables (that may come at a later
stage), but simply explain how to use them. Given the ease of setting up
databases, though, a tutorial on the absolute basics would be like teaching your
granny to suck eggs.
If you have any questions regarding this tutorial (written exclusively for
BioRUST) then please contact me on the forums by clicking on my name below and
following the links to either Private Message or Email (you may
have to register). Bye for now!
- Tutorial written by Scrowler
| 
There are no comments for this tutorial yet. You can place a comment by clicking here.
|

|
|
 |
TrentonCS Author: ahstanford Posted: Nov 06th, 7:40pm Activity: 3 replies, 28 views
|  | Html 5 Author: ahstanford Posted: Nov 05th, 1:32pm Activity: 5 replies, 90 views
|  | What are your favorite websites? Author: ahstanford Posted: Nov 05th, 12:51am Activity: 0 replies, 47 views
|  | What do you do for a living? Author: ahstanford Posted: Nov 04th, 11:04pm Activity: 0 replies, 45 views
|  | What is your favorite Subway sandwich? Author: ahstanford Posted: Nov 04th, 11:02pm Activity: 4 replies, 89 views
|  | Windows 7 Author: ahstanford Posted: Nov 04th, 10:59pm Activity: 0 replies, 49 views
|  | Google Wave Author: ahstanford Posted: Nov 04th, 10:52pm Activity: 0 replies, 60 views
|  | University Project Author: Gjbphp Posted: Nov 03rd, 8:59pm Activity: 1 replies, 91 views
|  | Hello BioRust! Author: ahstanford Posted: Nov 02nd, 5:39pm Activity: 4 replies, 87 views
|  | Illustrator cs4 - Convert outlines/graphics to ... Author: izidrew Posted: Oct 29th, 3:48pm Activity: 3 replies, 262 views
|  | Hello BioRust!! Author: MOST Posted: Oct 29th, 12:52am Activity: 4 replies, 144 views
|  | Hey! newbie Author: prelimiting Posted: Oct 28th, 11:51pm Activity: 1 replies, 113 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|