However, before we can start programming - I want to teach you how to think in databases.
A database (or 'DB' as they're sometimes called) is basically like a large file cabinet for different folders. Here is what a DB would look like as a picture:
The Database is called database_1 and inside the database are two tables "contacts" and "orders". Both of those tables have 3 columns in them:
The table "orders" has the columns "id", "date", and "item".
The table "contacts" has the columns "name", "phone", and "address".
In the end, all of the information in a database is stored in one column or another; the whole point of tables is just to help group columns that have similar or related information.
After each of the columns that will be in a table are made, you start getting into rows. (Think of an Excel Spreadsheet made up of rows and columns, or for those of you who have done HTML; think of
Name | 'Phone | 'Address | '
". $row[0]. " | ";". $row[1]. " | ";". $row[2]. " | ";
//////////
//NEW CODE
//////////
?>
Save the new file as "showdata.php" and upload it to your web server. When you run it you should see a table with Name, Phone, and Address at the top and below it is each row from the database.
The only code I need to explain is the while loop:
while($row = mysql_fetch_row($result)) {
echo ""; ";
echo "". $row[0]. " ";
echo "". $row[1]. " ";
echo "". $row[2]. " ";
echo "
}
This code would read like this in English:
while(the variable "$row" is equal to a_row_fetched_from_the($result)) {
print a new row in HTML ""; ";
print a new column in HTML "". then print out the value of the first element in $row ($row[0]). " ";
print a new column in HTML "". then print out the value of the second element in $row ($row[1]). " ";
print a new column in HTML "". then print out the value of the third element in $row ($row[2]). " ";
print a new end-row in HTML "
}
After, the loop runs one time it will go back to the start and it will see if there is another row in the result ($row = mysql_fetch_row($result)) if there is; then code will run again and if NOT it will stop.
I also would like to say that you could replace:
// Show data query
$query = 'SELECT * FROM `contacts`';
with:
// Show data query
$query = 'SELECT `name`, `phone`, `address` FROM `contacts`';
The script would still give the same result! This is because the first code says "SELECT ALL" and the second code says "SELECT `name`, `phone`, `address`" but since that is all of the columns in the table it is the same as saying "SELECT ALL"!
That's it!
-----------------------------------------------------------
-- Other MySQL Queries:
-----------------------------------------------------------
Here are some other queries you can do with MySQL.
DELETING:
DELETE FROM `contacts` WHERE name=bob;
DELETE * FROM `contacts`;
Update:
UPDATE `contacts` SET name='newbob' WHERE name='Bob';
Creating a Database:
CREATE DATABASE `newdatabase`;
Showing what Databases are Available:
SHOW DATABASES;
Showing what Tables are available in a database:
SHOW TABLES;
Select Everything from a table in a Database:
SELECT * FROM `yourtablename`;
Deleting a Database:
DROP DATABASE `databasename`;
-------------------------------------------------------------
Next lesson I'll show you how to do more advanced things with databases.
(We'll also get rid of all of that data with DELETE )