| 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.
|

|
|
 |
php, shoutbox problems Author: vanhansen Posted: Nov 17th, 1:30am Activity: 5 replies, 106 views
|  | MarkupGeeks Logo Author: ahstanford Posted: Nov 16th, 8:45pm Activity: 13 replies, 197 views
|  | Drawing Tutorials Author: ahstanford Posted: Nov 16th, 12:46am Activity: 0 replies, 119 views
|  | Superbowl predictions, anyone? Author: ahstanford Posted: Nov 15th, 10:46pm Activity: 10 replies, 171 views
|  | Photomanipulation Footsteps Author: ahstanford Posted: Nov 15th, 10:43pm Activity: 4 replies, 122 views
|  | Learning to draw... Author: ahstanford Posted: Nov 15th, 12:43pm Activity: 4 replies, 138 views
|  | Looking for simple UI elements Author: FenixRoA Posted: Nov 15th, 6:40am Activity: 7 replies, 128 views
|  | HDD Help? Author: Phoenix Wynde Posted: Nov 13th, 2:31am Activity: 1 replies, 126 views
|  | Fun New Battles Posted! Author: ahstanford Posted: Nov 11th, 7:33pm Activity: 0 replies, 156 views
|  | 4-man Simon Tournament Author: ahstanford Posted: Nov 11th, 3:28pm Activity: 0 replies, 103 views
|  | Design Brief Inspiration for BioRUST Battles! Author: ahstanford Posted: Nov 11th, 7:19am Activity: 4 replies, 153 views
|  | The BioRUST Free Stock Photography Thread Author: ahstanford Posted: Nov 11th, 6:32am Activity: 2 replies, 173 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|