Introduction to PHP DATA BASE
Learning Objectives
At the end of this topic, you will be able to:
- Explain MySQL and its features
- Explain MySQL database connectivity with PHP
- Explain syntaxes related to the database
- Describe how to update values in a table using PHP
MySQL Introduction
- MySQL is the popularly used freely available relational database management system (RDMS)
- You communicate with the database through queries, The communication usually involves making requests to the database to perform read/write operations. These queries are written in Structured Query Language
Important Features
- MySQL is reliable, fast and east to use.
- It can be used for smaller as well as larger projects handling huge volume of data.
- It compiles on a number of platforms.
- It is one of the building blocks of the popular LAMP Architecture.
Connecting and retrieving data-Introduction
While using PHP, if you want to perform any
operation on the database data you need to first establish a database
connectivity. This enables communication between PHP and MySQL.
How do you establish a connection?
- Connection is not established in-built and has to be created explicitly
- You use the mysql_connect() function to connect PHP and MySQL
- The mysql_connect() function, also called as data base handle, returns a resource which is a pointer to the database connection
mysql_connect()
Function
The syntax for
myqsl_connect function is as follows:
Syntax
mysql_connect($hostname, $username, $password)
$hostname: This is the local host as the MySQL is
installed on local machine.
$username: This is the username to connect to
databases.
$password: This is the password to connect to the
databases, it is not mandatory to have password.
Note: You have
to give the parameters and remember them as they are required for establishing
connections later.
Example of PHP and MySQL
Connection Using mysql_connect() Function
- Write the following script in Notepad and save it with .php extension.
<?php
$username
= "xyz";
$hostname
= "localhost";
$dbhandle
= mysql_connect ($hostname, $username);
echo
"Connected to MySQL";
?>
- Run the file in a Web browser. If a connection is established, it displays the message connection established.