Many peoples have problems with PHP and MySQL programming in the beginning, specially those who don't understead the simple mechanism of the MySQL <-> PHP interaction.
I'll try to explain in the most simplified and partice way i can, to try solving those questions.
Explaination
What is one Database?
I'll explain assuming the following situation:
Imagine how many clients does the telephonic companies have? Now did you thinked if it store the informations of the clients in plain-text files? Thinking that one Character is approx. 1 byte, it'll use many HDs to store.
So this is the function of the Databases... The databases are magnific tools, that are capable to handle mass information/data.
One of main caracteristic of the Databases is the data compression. For example, MySQL have one table type called "LongBLOB" that can store 4,2GB of data!
The databases are used since Websites on Internet, to offices, banks, big companies, to store many types of data (generally mass information).
Theres many databases, like MySQL, Postgree, Oracle, Microsoft SQL Server, etc.
The MySQL Database
Its first bersion was developed in 1979, under the license GNU, a FREE software, so thats the reason for the popularity on Internet.
The PHP
Had its first version in 1996, by the Prof. of Science Computation Rasmus Lerdorf. In the epoch it has only simple functions, like creating website counters, guestbooks, etc... With the time many peoples modified PHP, so, great groups like Zend until it be what is currently, a great cross-plataform website/shell programming language.
The PHP-Database Relation
Originally the databases wasn't created to work together with PHP, in a colletive effort, the peoples adapted the PHP to handle Databases, adding special functions to help in handling.
The Start
First i'll start the explaination by the main working way of a database.
- Databases: A kind of "Folders" that store the tables.
- Tables: Are called as this because they store the information in tables, for example:
- Code: Select all
+-------------+-----------+
| Fruit | Color |
+-------------+-----------+
| Orange | Orange |
| Lemon | Green |
| Pineapple | Yellow |
+-------------------------+
Lets get Started...
I'll teach how to use PHP to insert, update, delete and display the Database's data. Its highly recommended that you've some knowledge in PHP.
Assuming the follow database:
- Code: Select all
Database Name: test_db
Table Name : cars
+---------+--------+------+
| Car | Color | Year |
+---------+--------+------+
| Audi | Silver | 2000 |
| Mazda | Yellow | 2003 |
| Ferrari | Red | 2006 |
+---------+--------+------+
Connecting to the Database:
Before execute any command on MySQL, you must connect to it. The commands to connect are:
- mysql_connect("Server_Name", "Username", "Password");
Ex.: $connection = mysql_connect("localhost", "root", "root");
After the connection , you MUST select the Database:
- mysql_select_db("database_name", $connection);
So, our code:
- Code: Select all
$connection = mysql_connect("localhost", "root", "root");
mysql_select_db("test", $connection;
Code Started, so we need to know some SQL (Structured Query Language), pretty easy for those who knows english:
- SELECT
- FROM
- * = Wildcard, means "ALL"
- VALUES
- DELETE
- WHERE
- UPDATE
- ORDER BY
- LIMIT
Those was some keywords for the SQL, with they are literally possible to do phrases with instructions for the database, its a kind of simplifed english. For example:
The Query "SELECT * FROM cars", will mean "Select all from the table cars".
The function in PHP to do MySQL queries is the mysql_query:
- Code: Select all
$query = mysql_query("SELECT * FROM cars", $connection);
PS: If the command uses SELECT, the Database will output Data, so you'll need to store it in a database to handle it soon...
Inserting Data
Well, now you know sufficiently to understand:
- Code: Select all
<?php
$connection = mysql_connection("server_name", "username", "password");
mysql_select_db("cars");
mysql_query("INSERT INTO carros('car', 'year', 'color') VALUES ('Audi', 'Silver', 2000)");
?>
Updating Data
To change data:
- Code: Select all
<?php
$connection = mysql_connection("server_name", "username", "password");
mysql_select_db("cars");
mysql_query("UPDATE cars SET color='Black', year='2002' WHERE car='Audi';");
?>
Deleting Data
To delete data:
- Code: Select all
<?php
$connection = mysql_connection("server_name", "username", "password");
mysql_select_db("cars");
mysql_query("DELETE * FROM cars WHERE car='Audi';");
?>
Displaying Data
Assuming that you wan't to know how are the cars in the Database:
- Code: Select all
<?php
mysql_connect("localhost", "root", "root");
$query = mysql_query("SELECT * FROM cars", $connection);
while ($data = mysql_fetch_array($query)) {
$year = $data['year];
$color = $data['color'];
echo "<hr>Color: ".$color."<br>Year: ".$year."<hr>";
}
?>
Understeading the Code
- mysql_fetch_array() - Executes the query, and retrieve the Database's output array for the SELECT type query to the variable $data. This returned array should be filled to the variables and displayed in a While/For loop.
Thats It, Hope you've Enjoyed.
Bye...[/b]




