MySQL Basics Tutorial
Ranked #3,201 in Internet, #181,219 overall
What is MySql?
Pre-Knowledge
In order to fully understand this tutorial you should already have mastered the PHP basics and have some basic understanding about arrays and functions.
Creating a database
How to create a mysql database and tables in it using phpmyadmin.
- To access phpmyadmin, go to localhost/phpmyadmin.
- Create a database ('test_database') as shown in the picture below.
- Create a table ('products', 5 fields) as shown in the picture below.
- Create the fields for the table as shown in the picture below.

- Make sure the ID field is unique by making it primary key and make it auto-increment as shown in the picture below.
Connecting to the database
How to connect to the mysql server and our database.
mysql_connect("localhost", "root", "");
now we need to select the database ( the one we just created ). We can use the php function mysql_select_db to do this.mysql_select_db("test_database");
And that's it, we established a connection with the mysql database. Now we can start interacting with our database, using mysql queries.Interacting with the database
Using mysql queries to interact with our database.
$query = mysql_query("query");
The query to be executed is put between brackets as argument for the function. The 3 types of queries are: insert queries, update queries and select queries.Insert Query
mysql_query("INSERT INTO table_name VALUES(field1, field2, field3)VALUES('value1', 'value2', 'value3')");
The insert query is used to put data inside of a table in the database ( create entries to the mysql table ). Using the table 'products' we made, we could for example run the following query to add a product called 'Samsung Galaxy S II' with description 'A great smartphone.', category 'Mobile Phones' and price '599.95'. The ID is automaticly generated as we set that field to be the primary key and auto-increment ( which means it will start at 1, and for each new entry made to the table it will increase by 1 ).mysql_query("INSERT INTO products(name, description, category, price)VALUES('Samsung Galaxy S II', 'A great smartphone', 'Mobile Phones', '599.95') ");
Update Query
mysql_query("UPDATE table_name SET field1 = 'value1', field2 = 'value2', field3 = 'value3' WHERE clause");
(The 'WHERE clause' means the condition for the entries to update, for example: 'WHERE id = 2', but is not necessary: if there's no 'WHERE'-part, all entries wil be updated/affected.)The update query is used to update one (or multiple) specific entries from a table in the database. Using the table 'products' we made and the product we just added with our MySql INSERT query, we could for example run the following query to update the price of the product 'Samsung Galaxy S II'.
mysql_query("UPDATE products SET price = '549.95' WHERE name = 'Samsung Galaxy S II' ");
We now updated all entries with the name 'Samsung Galaxy S II' and updated the price for those entries ( products ) to '549.95'. However it's more precise to do it by ID, as the ID of a product/entry in the mysql table is always unique. So we could also use the ID of the product ( for example 1 ) to make sure we update that specific product only:mysql_query("UPDATE products SET price = '549.95' WHERE id = 1");
Select Query
mysql_query("SELECT field1, field2, field3 FROM table_name WHERE clause");
(The 'WHERE clause' means the condition for the entries to select (specifiements), for example: 'WHERE id = 2', but is not necessary: if there's no 'WHERE'-part, all entries wil be selected.)The select query is used to select entries from a table in the database ( or combine multiple tables ). Using the table 'products' we made and the product we just added to this table, we can select that specific product using this query:
mysql_query("SELECT * FROM products WHERE id = 1");
or if we select it by its name:mysql_query("SELECT * FROM products WHERE name = 'Samsung Galaxy S II'");
We used the * sign to select ALL fields (id,name,price,description). We could, though, also select just a few fields or only the price for example:mysql_query("SELECT price FROM products WHERE name = 'Samsung Galaxy S II'");
To actually catch the results of this query, we can use the PHP function mysql_fetch_assoc, which catches all results that were selected by the query given as argument to the function as an array. In this case we just selected one entry ( the product 'Samsung Galaxy S II' ) so we could catch it in one variable:$product = mysql_fetch_assoc(mysql_query("SELECT * FROM products WHERE id = 1"));
Now $product will be an array containing the result found, which would be this array in this case:array(
"id" => 1,
"name" => "Samsung Galaxy S II",
"description" => "A great smartphone.",
"price" => "549.95"
);
$query = mysql_query("select query");
$result = mysq_fetch_assoc($query);
$field_value = $result['field_name'];
<?php
$query = mysql_query("SELECT * FROM products"); //select ALL products
//handle each entry found, having 1 entry as array in $product per time the while loop is executed
while($product = mysql_fetch_assoc($query)) {
$products[] = $product;
}
?>
Featured Lenses
Comments & Questions
Got any questions about the tutorial or just want to comment? This is the place to do so.
-
-
Aug 19, 2011 @ 1:39 pm | delete
- I was planning on programming a customer database using mysql. Your lens came in very useful. Thanks.
-
-
-
dogface
Aug 19, 2011 @ 12:53 pm | delete
- I'm glad there's phpMyAdmin out there as it makes things lots and lots of easier to do.
-
-
-
Tolovaj
Jul 13, 2011 @ 10:22 pm | delete
- Looks like a tutorial which can be very helpful for a database novice like me... Thanks!
-
-
-
catherinelovestv
Jul 6, 2011 @ 3:53 pm | delete
- Thanks for the info, its hard to find tutorials on php and sql
-
-
-
catherinelovestv
Jul 6, 2011 @ 3:53 pm | delete
- Thanks for the info, its hard to find tutorials on php and sql
-
Related Products (Amazon)
Related Products (eBay)
by webcodez
"Give a man a program and you'll frustrate him for one day. Teach a man to program and you'll frustrate him for a whole lifetime."
webcodez
GameHeroes
more »
- 21 featured lenses
- Winner of 14 trophies!
- Top lens » PHP & MySQL Poll System Tutorial
Explore related pages
- PHP basics tutorial PHP basics tutorial
- PHP & MySQL Poll System Tutorial PHP & MySQL Poll System Tutorial
- PHP & MySQL Visitors Counter Tutorial PHP & MySQL Visitors Counter Tutorial
- PHP & MySQL Login System Tutorial PHP & MySQL Login System Tutorial
- HTML Basics Tutorial HTML Basics Tutorial
- PHP Banner Rotator Tutorial PHP Banner Rotator Tutorial