.
Tutorial
Avatar

PHP basic lesson 10

By Remco on 2009-04-25
Views: 1207

Introduction

This tutorial is going to teach you how to create a database with PHP,
fill the database and retrieve information from it.

This tutorial requires you to have read PHP basic tutorial 9 or that you know how to make a connection to your Mysql database.

Step 1 - Create PHP files

In the first step you're going to create the PHP files.

In this tutorial we're going to work with:

- connect.php
- create.php
- insert.php
- retrieve.php


Step 2 - Make the connection

Open file: connect.php
We are now just going to make a connection to the database which is required to interact with the database in any way.
Copy and paste the code below and fill in your own database details:


Code:


<?php 

$host 
"localhost"// mysql host 
$dbuser "root"// mysql username 
$pass "pass"// mysql password 

mysql_connect($host,$dbuser,$pass)or die('Could not connect: '.mysql_error()); 

?>




Save the file and close it.

Step 3 - Create a database

Open file: create.php
There are many ways to create a database, by using tools such as phpmyadmin or mysql query browser etc.
We are going to use PHP because it will also allow you to make things dynamicly if you wish.

To make a database, fill a database, retrieve from a database etc. you will have to use "queries", queries are basicly questions you ask to a database and the database will answer them.

Information

PHP comes with built in functions for mysql,
to execute a query we are going to use this function:
mysql_query();


This is what we are going to do in create.php:
( DO NOT EXECUTE THE FILE YET )


Code:


<?php

include('connect.php'); // connects to the database
    
$databasename "database";
    
/* Below here we are going to make the database by using a query */
mysql_query("CREATE DATABASE `".$databasename."` ");
    
?>




If you would run this script you would only have an empty database.
A database contains tables, tables will contain the information you want to put in. So we are going to create a user table in our database with another query.



Code:


<?php

include('connect.php'); // connects to the database

$databasename "database";
    
/* Below here we are going to make the database by using a query */
mysql_query("CREATE DATABASE `".$databasename."` ");

/* Below here we are going to make the user table by using a query */
mysql_query("
CREATE TABLE `"
.$databasename."`.`users`(
user_id int(10) AUTO_INCREMENT,
firstname varchar(255),
lastname varchar(255),
email varchar(255),
PRIMARY KEY (user_id)
)"
);
    
?>




I will explain what is happening up here.
In the queries we have used the CREATE DATABASE and the CREATE TABLE commands to create a database with 1 table to store the users in.

When we create a table we have to specify column names (user_id etc.), a variable sort (INT, VARCHAR etc.), if necessary a length (10,255 what ever you want.).
We have also specified that user_id is an auto increment column which means it will automaticly go to the next number: 1,2,3,4,5 etc.
The primary key at the bottom specifies the "identifier" of the in this case user.
The user_id will always be unique which make it the primary key of this table cos you can identify a user with it.

Now you can run the script by browsing to it with your web browser.
If the browser doesnt output anything.. it means everything has gone well.
If you get an error message, please contact one of the programmers on this website, or me: remco.borst@live.nl


Step 4 - Change the connect file

Open file: connect.php
Now we have made the database we have to tell PHP that we want to use that database to interact with.

add this line to your code and put in your database name:


Code:

mysql_select_db(".$databasename.") or die(mysql_error());


save the file and close it.

Step 5 - Fill your database

Open file: insert.php
We are now going to execute an "INSERT query" which will allow us to add a user.

put this code in the file and run it:


Code:


<?php

include('connect.php'); // connects to the database
mysql_query("INSERT INTO users (firstname,lastname,email) VALUES('Remco','Borst','remco.borst@live.nl') ");

?>



You have now got 1 user in your database.

Step 6 - retrieving information

Open file: retrieve.php
With this file we are going to retrieve the information we have just inserted into the database.

to do so we will have to perform a query again, but we are also going to use a new mysql function:
mysql_fetch_array();

This function will automaticly put the results of your query in an array.
We will explain more about this soon.

Use this code:


Code:


<?php

include('connect.php'); // connects to the database

$get_users mysql_query("SELECT * FROM users");
$row_users mysql_fetch_array($get_users);

echo 
$row_users['firstname']." ".$row_users['lastname']." ".$row_users['email'];

?>




As you see we use variables now for our query.
This is very usefull because we need the query information again to create the automatic array.

in this case variable $row_users is an array of users, this array will contain the same names as the table columns of our database as you can see (firstname,lastname,email)

to output this to the browser we just specify what part of the array we want to show by saying something like: $row_users['firstname'].

Save the file and run it, you should see:
Remco Borst remco.borst@live.nl


I hope this tutorial was useful to you, and i hope i was clear enough.
« Previous Next »




Comments

You have to be logged in to write a comment.

.