Thursday, 9 April 2015

How to use PHP to access a Database

Databases are backbone of today's websites. PHP has support for over 20 databases. MySQL, PostgreSQL and Oracle are widely used databases. Databases are used for storing users registration data, credit card numbers, purchase history, product reviews and many other things.
PHP and phpmyadmin both are used to access databases. Relational database management system is a server which is used to manage data. Data is structured into tables where each table have some no. of columns, each of which has a name and a type. PHP communicates with relational databases such as MySQL and Oracle using structured query language (SQL). The syntax for SQL is divided into two parts. The first, Data Manipulation Language, or DML, is used to retrieve and modify data in an existing database. DML is remarkably compact, consisting of only four verbs: select, insert, update, and delete. The set of SQL commands, used to create and modify the database structures that hold the data, is known as Data Definition Language, or DDL. The syntax for DDL is not as standardized as that for DML, but as PHP just sends any SQL commands you give it to the database, you can use any SQL commands your database supports.

Assuming you have a table called movies, this SQL statement would insert a new row:
INSERT INTO movies VALUES(0, 'fast and furious 7', 2015, 2)
This SQL statement inserts a new row but lists the columns for which there are values:
INSERT INTO movies (title, year, actor) VALUES ('hercules', 2015, 2)
To delete all movies from 2015, we could use this SQL statement:
DELETE FROM movies WHERE year=2015
To change the year for hercules to 2014, use this SQL statement:
UPDATE movies SET year=2014 WHERE title='hercules'
To fetch only the movies made in the 2010s, use:
SELECT * FROM movies WHERE year >= 2010 AND year < 2015 .

 Example :  

This example will show you how to insert form data into database using PHP.

1) First create a html form.

2) Now create a connect.php file.

No comments:

Post a Comment