PHP and MySQL
Delivered by
Joel Anandraj.E
AP/IT
Benefits of using PHP and MySQL
What is MySQL?
• MySQL is a database system used on the web.
• The data in a MySQL database are stored in tables.
• A table is a collection of related data, and it consists of columns and
rows.
• Databases are useful for storing information categorically.
• A company may have a database with the following tables:
• Employees
• Products
• Customers
• Orders
• One of the main reasons the businesses choose PHP MYQL
Application Development is its simplicity and ease of use.
Advantages of PHP MYSQL Web Development:
• Dynamic
• HTML embedded codes
• Ideal for ecommerce development
• Cost effective
• Interoperability
Database Queries
• A query is a question or a request.
• We can query a database for specific information and have a
recordset returned.
• Look at the following query (using standard SQL):
SELECT LastName FROM Employees
• The query above selects all the data in the "LastName" column from
the "Employees" table.
MySQL Connection Using PHP Script
• PHP provides mysql_connect() function to open a database
connection.
• This function takes five parameters and returns a MySQL link
identifier on success or FALSE on failure.
Syntax:
• mysql_connect(server,user,passwd,new_link,client_flag);
Create a Database using PHP Script
• PHP uses mysql_query function to create or delete a MySQL database.
• This function takes two parameters and returns TRUE on success or
FALSE on failure.
Syntax:
• bool mysql_query( sql, connection );
Selecting a MySQL Database Using PHP Script
• Once you get connected with the MySQL server, it is required to
select a database to work with.
• This is because there might be more than one database available with
the MySQL Server.
• PHP provides function mysql_select_db to select a database.
• It returns TRUE on success or FALSE on failure.
• Syntax
bool mysql_select_db( db_name, connection );
Creating Tables Using PHP Script
• To create new table in any existing database you would need to use
PHP function mysql_query().
Insert Data into MySQL Database
• Data can be entered into MySQL tables by executing SQL INSERT
statement through PHP function mysql_query.
• A simple example to insert a record into employee table.
Getting Data From MySQL Database
• Data can be fetched from MySQL tables by executing
SQL SELECT statement through PHP function
mysql_query.
• There are several options to fetch data from MySQL.
• The most frequently used option is to use function
mysql_fetch_array().
• This function returns row as an associative array, a numeric array, or
both.
• This function returns FALSE if there are no more rows.
• The following example displays all the records from employee table.
Deleting Data from MySQL Database :
• Data can be deleted from MySQL tables by executing SQL DELETE
statement through PHP function mysql_query.
• To delete a record in any table it is required to locate that record by
using a conditional clause.
• Example
Updating Data into MySQL Database
• Data can be updated into MySQL tables by executing
SQL UPDATE statement through PHP function
mysql_query.
• To update a record in any table it is required to locate that record by
using a conditional clause.
• Below is a simple example to update records into employee table.
Counting the number of Records
• The mysql_num_rows() function returns the number of rows in a result
set.
<?php
$con = mysql_connect("localhost", "root", "mypass");
$selectdb = mysql_select_db("tutorials",$con);
$result = mysql_query("select * from tutorials");
$number_of_rows = mysql_num_rows($result);
echo "Number of rows fetched are : ". $number_of_rows;
?>
Counting the number of Fields
• The mysql_num_fields() is used to get number of fields in a result.
<?php
$con = mysql_connect("localhost", "root", "mypass");
$selectdb = mysql_select_db("tutorials",$con);
$result = mysql_query("select name,no_of_pages,no_of_pages
from tutorials where t_id = '5'");
echo mysql_num_fields($result); // since three fields are fetched,
retutns 3
?>
Finding the number of affected rows:
• The mysql_affected_rows() function is used to get the number of
affected rows by the last MySQL query.
• If you run a mysql query to insert, update, replace or delete records,
and want to know how many records are being affected by that
query, you have to use mysql_affected_rows().
Example:
<?php
$con=mysql_connect('localhost', 'root', 'mypass');
//connecting to mysql using hostname. username amnd password.
mysql_select_db("tutorials")or die("cannot select DB");
//selecting 'tutorials' database
mysql_query("insert into tutorials(t_id,name,no_of_pages,no_of_examples,author) values('001','html',
'1000', '1000','rd')");
echo "Inserted records:".mysql_affected_rows();
echo "<br />It shows 1 if the new rows are inserted";
?>
Thank you