SlideShare a Scribd company logo
Chapter
MySQL Database Connection
 Opening Database Connection
 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: connection mysql_connect(server,user,passwd,new_link,client_flag);
Cont’d
Cont’d
Cont’d
 NOTE − You can specify server, user, passwd in php.ini file instead of using them again and
again in your every PHP scripts. Check php.ini file configuration.
 Closing Database Connection
 PHP provides the simplest function mysql_close() to close a database connection.
 This function takes connection resource returned by mysql_connect() function.
 It returns TRUE on success or FALSE on failure.
Syntax: bool mysql_close ( resource $link_identifier );
If a resource is not specified then last opend database connection will be closed.
Example
Try out following example to open and close a database connection −
<?php
$dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
} else
echo 'Connected successfully';
mysql_close($conn); ?>
Cont’d
Cont’d
Try out following example to create a database −
<?php
$dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
echo 'Connected successfully';
$sql = 'CREATE Database test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not create database: ' . mysql_error());
} echo "Database test_db created successfullyn";
mysql_close($conn); ?>
Cont’d
 Selecting a Database
 Once you establish a connection with a database server then it is required to select a
particular database where all your tables are associated.
 This is required because there may be multiple databases residing on a single server and you
can do work with a single database at a time.
 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 );
Cont’d
Example
Here is the example showing you how to select a database.
<?php
$dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
echo 'Connected successfully';
mysql_select_db( 'test_db' );
mysql_close($conn); ?>
Creating Database Tables
 To create tables in the new database you need to do the same thing as creating the database.
 First create the SQL query to create the tables then execute the query using mysql_query() function.
Example
 Try out following example to create a table −
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
echo 'Connected successfully';
Cont’d
$sql = 'CREATE TABLE employee( '.
'emp_id INT NOT NULL AUTO_INCREMENT, '.
'emp_name VARCHAR(20) NOT NULL, '.
'emp_address VARCHAR(20) NOT NULL, '.
'emp_salary INT NOT NULL, '.
'join_date timestamp(14) NOT NULL, '.
'primary key ( emp_id ))';
mysql_select_db(‘testdatabase');
$retval = mysql_query( $sql, $conn );
if(! $retval ) { die('Could not create table: ' . mysql_error()); }
echo "Table employee created successfullyn";
mysql_close($conn); ?>
Cont’d
 In case you need to create many tables then its better to create a text file first and put all the SQL
commands in that text file and then load that file into $sql variable and excute those commands.
 Consider the following content in sql_query.txt file
CREATE TABLE employee(
emp_id INT NOT NULL AUTO_INCREMENT,
emp_name VARCHAR(20) NOT NULL,
emp_address VARCHAR(20) NOT NULL,
emp_salary INT NOT NULL,
join_date timestamp(14) NOT NULL,
primary key ( emp_id ));
Cont’d
<?php
$dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
$query_file = 'sql_query.txt';
$fp = fopen($query_file, 'r');
$sql = fread($fp, filesize($query_file));
fclose($fp);
mysql_select_db(‘testdatabase');
$retval = mysql_query( $sql, $conn );
if(! $retval ) { die('Could not create table: ' . mysql_error()); }
echo "Table employee created successfullyn";
mysql_close($conn); ?>
Deleting MySQL Database Using PHP
 Deleting a Database
 If a database is no longer required then it can be deleted forever.
 You can pass an SQL command to mysql_query to delete a database.
Example
Try out following example to drop a database.
Cont’d
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
$sql = 'DROP DATABASE test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval ) { die('Could not delete database db_test: ' . mysql_error()); }
echo "Database deleted successfullyn";
mysql_close($conn); ?>
Deleting a Table
 Its again a matter of issuing one SQL command through mysql_query function to delete any
database table. But be very careful while using this command because by doing so you can
delete some important information you have in your table.
Example
Try out following example to drop a table −
Cont’d
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
$sql = 'DROP TABLE employee';
$retval = mysql_query( $sql, $conn );
if(! $retval ) { die('Could not delete table employee: ' . mysql_error()); }
echo "Table deleted successfullyn";
mysql_close($conn); ?>
Insert Data into MySQL Database
 Data can be entered into MySQL tables by executing SQL INSERT statement through PHP function mysql_query.
Below a simple example to insert a record into employee table.
<?php
$conn = mysql_connect(localhost:3036', ‘root’, ‘rotpassword’);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
$sql = 'INSERT INTO employee '. '(emp_name, emp_address, emp_salary, join_date) '. 'VALUES ( "guest", "XYZ",
2000, NOW() )';
mysql_select_db(testdatabase');
$retval = mysql_query( $sql, $conn );
if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
echo "data Entered successfullyn";
mysql_close($conn); ?>
Cont’d
 In real application, all the values will be taken using HTML form and then those values will
be captured using PHP script and finally they will be inserted into MySQL tables.
 While doing data insert its best practice to use function get_magic_quotes_gpc() to check if
current configuration for magic quote is set or not.
 If this function returns false then use function addslashes() to add slashes before quotes.
Example check out add_employee.php file

More Related Content

PPTX
3-Chapter-Edit.pptx debre tabour university
PPT
MYSQL - PHP Database Connectivity
PPTX
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
PDF
PHP with MySQL
PDF
PHP and Mysql
PPT
PHP - Getting good with MySQL part II
PPTX
Learn PHP Lacture2
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
3-Chapter-Edit.pptx debre tabour university
MYSQL - PHP Database Connectivity
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
PHP with MySQL
PHP and Mysql
PHP - Getting good with MySQL part II
Learn PHP Lacture2
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College

Similar to chapter_Seven Database manipulation using php.pptx (20)

PPTX
UNIT V (5).pptx
PPTX
Php mysq
PPTX
CHAPTER six DataBase Driven Websites.pptx
PDF
Php 2
PPT
9780538745840 ppt ch08
PPTX
PHP and MySQL.pptx
PPTX
Database Connectivity in PHP
PPTX
Connecting_to_Database(MySQL)_in_PHP.pptx
PPT
Php with MYSQL Database
PPT
MySQLi - An Improved Extension of MySQL
PPT
My sql with querys
PPTX
Web Application Development using PHP Chapter 7
PPT
Php and MySQL Web Development
PPT
Download It
ODP
PPT
Chapter 09 php my sql
PPT
PPT
Php MySql For Beginners
UNIT V (5).pptx
Php mysq
CHAPTER six DataBase Driven Websites.pptx
Php 2
9780538745840 ppt ch08
PHP and MySQL.pptx
Database Connectivity in PHP
Connecting_to_Database(MySQL)_in_PHP.pptx
Php with MYSQL Database
MySQLi - An Improved Extension of MySQL
My sql with querys
Web Application Development using PHP Chapter 7
Php and MySQL Web Development
Download It
Chapter 09 php my sql
Php MySql For Beginners
Ad

More from Getawu (7)

PPTX
5 Protect-Application-or-System-Software (2).pptx
PDF
moniteradministratornetworksystemsecurity-231020035832-51b6b47f (1).pdf
PPTX
Develop Keyboard Skill.pptx er power point
PPT
Identifying and categorizing computer software
PPTX
: Connect hardware peripherals Part one
PPTX
Connect a workstation to the internet ppt
PPTX
Hardware and Networking Service Document PPT
5 Protect-Application-or-System-Software (2).pptx
moniteradministratornetworksystemsecurity-231020035832-51b6b47f (1).pdf
Develop Keyboard Skill.pptx er power point
Identifying and categorizing computer software
: Connect hardware peripherals Part one
Connect a workstation to the internet ppt
Hardware and Networking Service Document PPT
Ad

Recently uploaded (20)

PPT
Introduction Database Management System for Course Database
PDF
medical staffing services at VALiNTRY
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
System and Network Administraation Chapter 3
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Transform Your Business with a Software ERP System
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Introduction Database Management System for Course Database
medical staffing services at VALiNTRY
Which alternative to Crystal Reports is best for small or large businesses.pdf
Digital Systems & Binary Numbers (comprehensive )
2025 Textile ERP Trends: SAP, Odoo & Oracle
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms I-SECS-1021-03
history of c programming in notes for students .pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Digital Strategies for Manufacturing Companies
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Odoo Companies in India – Driving Business Transformation.pdf
PTS Company Brochure 2025 (1).pdf.......
System and Network Administraation Chapter 3
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Reimagine Home Health with the Power of Agentic AI​
Transform Your Business with a Software ERP System
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus

chapter_Seven Database manipulation using php.pptx

  • 2. MySQL Database Connection  Opening Database Connection  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: connection mysql_connect(server,user,passwd,new_link,client_flag);
  • 5. Cont’d  NOTE − You can specify server, user, passwd in php.ini file instead of using them again and again in your every PHP scripts. Check php.ini file configuration.  Closing Database Connection  PHP provides the simplest function mysql_close() to close a database connection.  This function takes connection resource returned by mysql_connect() function.  It returns TRUE on success or FALSE on failure. Syntax: bool mysql_close ( resource $link_identifier ); If a resource is not specified then last opend database connection will be closed.
  • 6. Example Try out following example to open and close a database connection − <?php $dbhost = 'localhost:3036'; $dbuser = 'guest'; $dbpass = 'guest123'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } else echo 'Connected successfully'; mysql_close($conn); ?>
  • 8. Cont’d Try out following example to create a database − <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $sql = 'CREATE Database test_db'; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not create database: ' . mysql_error()); } echo "Database test_db created successfullyn"; mysql_close($conn); ?>
  • 9. Cont’d  Selecting a Database  Once you establish a connection with a database server then it is required to select a particular database where all your tables are associated.  This is required because there may be multiple databases residing on a single server and you can do work with a single database at a time.  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 );
  • 11. Example Here is the example showing you how to select a database. <?php $dbhost = 'localhost:3036'; $dbuser = 'guest'; $dbpass = 'guest123'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_select_db( 'test_db' ); mysql_close($conn); ?>
  • 12. Creating Database Tables  To create tables in the new database you need to do the same thing as creating the database.  First create the SQL query to create the tables then execute the query using mysql_query() function. Example  Try out following example to create a table − <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully';
  • 13. Cont’d $sql = 'CREATE TABLE employee( '. 'emp_id INT NOT NULL AUTO_INCREMENT, '. 'emp_name VARCHAR(20) NOT NULL, '. 'emp_address VARCHAR(20) NOT NULL, '. 'emp_salary INT NOT NULL, '. 'join_date timestamp(14) NOT NULL, '. 'primary key ( emp_id ))'; mysql_select_db(‘testdatabase'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not create table: ' . mysql_error()); } echo "Table employee created successfullyn"; mysql_close($conn); ?>
  • 14. Cont’d  In case you need to create many tables then its better to create a text file first and put all the SQL commands in that text file and then load that file into $sql variable and excute those commands.  Consider the following content in sql_query.txt file CREATE TABLE employee( emp_id INT NOT NULL AUTO_INCREMENT, emp_name VARCHAR(20) NOT NULL, emp_address VARCHAR(20) NOT NULL, emp_salary INT NOT NULL, join_date timestamp(14) NOT NULL, primary key ( emp_id ));
  • 15. Cont’d <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $query_file = 'sql_query.txt'; $fp = fopen($query_file, 'r'); $sql = fread($fp, filesize($query_file)); fclose($fp); mysql_select_db(‘testdatabase'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not create table: ' . mysql_error()); } echo "Table employee created successfullyn"; mysql_close($conn); ?>
  • 16. Deleting MySQL Database Using PHP  Deleting a Database  If a database is no longer required then it can be deleted forever.  You can pass an SQL command to mysql_query to delete a database. Example Try out following example to drop a database.
  • 17. Cont’d <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'DROP DATABASE test_db'; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not delete database db_test: ' . mysql_error()); } echo "Database deleted successfullyn"; mysql_close($conn); ?>
  • 18. Deleting a Table  Its again a matter of issuing one SQL command through mysql_query function to delete any database table. But be very careful while using this command because by doing so you can delete some important information you have in your table. Example Try out following example to drop a table −
  • 19. Cont’d <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'DROP TABLE employee'; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not delete table employee: ' . mysql_error()); } echo "Table deleted successfullyn"; mysql_close($conn); ?>
  • 20. Insert Data into MySQL Database  Data can be entered into MySQL tables by executing SQL INSERT statement through PHP function mysql_query. Below a simple example to insert a record into employee table. <?php $conn = mysql_connect(localhost:3036', ‘root’, ‘rotpassword’); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'INSERT INTO employee '. '(emp_name, emp_address, emp_salary, join_date) '. 'VALUES ( "guest", "XYZ", 2000, NOW() )'; mysql_select_db(testdatabase'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); } echo "data Entered successfullyn"; mysql_close($conn); ?>
  • 21. Cont’d  In real application, all the values will be taken using HTML form and then those values will be captured using PHP script and finally they will be inserted into MySQL tables.  While doing data insert its best practice to use function get_magic_quotes_gpc() to check if current configuration for magic quote is set or not.  If this function returns false then use function addslashes() to add slashes before quotes. Example check out add_employee.php file