KEMBAR78
chapter_Seven Database manipulation using php.pptx
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

chapter_Seven Database manipulation using php.pptx

  • 1.
  • 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);
  • 3.
  • 4.
  • 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 followingexample 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); ?>
  • 7.
  • 8.
    Cont’d Try out followingexample 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 aDatabase  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 );
  • 10.
  • 11.
    Example Here is theexample 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 = 'CREATETABLE 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 caseyou 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 DatabaseUsing 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 intoMySQL 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 realapplication, 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