[PHP Tutorial] PHP + MySQL for Beginners

Discussion about the creation of websites in general, HTML, PHP, Java.

This tutorial was useful for you?

Yes
4
80%
No
1
20%
 
Total votes : 5

[PHP Tutorial] PHP + MySQL for Beginners

Postby thestrifer on Sat Sep 09, 2006 1:03 pm

Hello people, this is my Second Tutorial.
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]
___________________
if ( $knowledge == $power) { $world = your; }
AMD Athlon 64 3400+ Socket 754 @ 2.43GHz
Asus K8V-X SE Offboard
1x512mb Ram Kingstone
NVidia GeForce FX5200 128MB AGP 8x
thestrifer
Contributer
Contributer
 
Posts: 21
Joined: Sat Sep 09, 2006 11:13 am
Location: Brazil

Postby Rage on Sun Oct 08, 2006 3:29 am

Hey thanks for the tutorial thestrifer, really appreciate it. Looks like I've learned a couple of new things. Cool
Rage
Contributer
Contributer
 
Posts: 31
Joined: Sun Oct 08, 2006 3:20 am

Postby trick on Sun Oct 08, 2006 8:48 pm

I didn't actually learn anything because I already know all this stuff, but it was one of the better explainations I've seen, so I voted yes.

Nice job.
'The military don't start wars. Politicians start wars' - William Westmoreland
Image
trick
Contributer
Contributer
 
Posts: 74
Joined: Mon Aug 07, 2006 6:07 am

Postby De Kus on Mon Nov 13, 2006 3:41 pm

just a little note:
for beginners to follow you, you might want to introduce them to CREATE TABLE, before giving them examples about SELECT * FROM, because it would simply lead to an error Wink.

PS:
also it would be okay of course to create an example table via an sql import using phpmyadmin (preinstealled on many mysql services) Very Happy.
Image
User avatar
De Kus
Phoenix Veteran
Phoenix Veteran
 
Posts: 305
Joined: Mon Mar 14, 2005 2:15 pm
Location: Germany


Return to General Webmastering

Who is online

Users browsing this forum: No registered users and 1 guest

cron