KEMBAR78
Web Application Development using PHP Chapter 7 | PPTX
CN5109
WEB APPLICATION
DEVELOPMENT
Chapter 7
MySQLi – Working with Database and Table
MySQLi PHP Syntax
• MySQLi works very well in combination of various
programming languages like PERL, C, C++, JAVA and PHP.
• Out of these languages, PHP is the most popular one because
of its web application development capabilities.
• The PHP functions for use with MySQLi have the following
general format:
mysqli_function(value,value,...);
MySQLi PHP Syntax
• Example:
<?php
$variable = mysqli_function(value, [value,...]);
if( !$variable )
{
Some codes … ;
}
// Otherwise MySQLi or PHP Statements
?>
MySQLi Functions
Function Description
mysqli_connect() Opens a new connection to the MySQL server
mysqli_error()
Returns the last error description for the most recent
function call
mysqli_query() Performs a query against the database
mysqli_fetch_assoc() Fetches a result row as an associative array
mysqli_close() Closes a previously opened database connection
MySQLi Connection
• Before we can access data in the MySQL database, we need to
be able to connect to the server.
• PHP provides mysqli_connect() function to open a database
connection.
• This function takes five parameters and returns a MySQLi link
identifier on success or FALSE on failure.
MySQLi Connection
• Example:
<?php
$servername = “ "; // localhost
$username = “ "; // your own server username
$password = “ "; // your own server password
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
MySQLi Create/Drop Database
• PHP uses mysqli_query function to create or delete a MySQLi
database.
• This function takes two parameters and returns TRUE on
success or FALSE on failure.
MySQLi Create Database
• Example:
<?php
$dbhost = ‘ ';
$dbuser = ‘ ';
$dbpass = ‘ ';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
echo 'Connected failure<br>';
}
echo 'Connected successfully<br>';
$sql = "CREATE DATABASE DB1";
if (mysqli_query($conn, $sql))
{
echo "Database created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
MySQLi Drop Database
• Example:
<?php
$dbhost = ‘ ';
$dbuser = ‘ ';
$dbpass = ‘ ';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
echo 'Connected failure<br>';
}
echo 'Connected successfully<br>';
$sql = “DROP DATABASE DB1";
if (mysqli_query($conn, $sql))
{
echo “Database deleted successfully";
}
else
{
echo "Error deleting database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
MySQLi Select Database
• Once you get connection with MySQLi server, it is required to
select a particular database to work with.
• This is because there may be more than one database
available with MySQLi Server.
MySQLi Select Database
• Example:
<?php
$dbhost = ‘ ';
$dbuser = ‘ ';
$dbpass = ‘ ';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
echo 'Connected failure<br>';
}
echo 'Connected successfully<br>';
mysqli_select_db( ‘DB1' );
//Write some code here
mysqli_close($conn);
?>
MySQLi Create Table
• The table creation command requires:
• Name of the table
• Names of fields
• Definitions for each field
• To create new table in any existing database you would
need to use PHP function mysqli_query().
• You will pass its second argument with proper SQL
command to create a table.
MySQLi Create Table
• Example:
<?php
// Connection to Database
// Select Database
$sql = "CREATE TABLE table1( id INT AUTO_INCREMENT,fname VARCHAR(20) NOT
NULL,primary key (id))";
if(mysqli_query($conn, $sql))
{
echo "Table created successfully";
}
else
{
echo "Table is not created successfully ";
}
mysqli_close($conn);
?>
MySQLi Drop Table
• It is very easy to drop an existing MySQLi table, but you
need to be very careful while deleting any existing table
because data lost will not be recovered after deleting a
table.
• To drop an existing table in any database, you would
need to use PHP function mysqli_query().
• You will pass its second argument with proper SQL
command to drop a table.
MySQLi Drop Table
• Example:
<?php
// Connection to Database
// Select Database
$sql = "DROP TABLE table1";
if(mysqli_query($conn, $sql))
{
echo "Table is deleted successfully";
}
else
{
echo "Table is not deleted successfullyn";
}
mysqli_close($conn);
?>

Web Application Development using PHP Chapter 7

  • 1.
    CN5109 WEB APPLICATION DEVELOPMENT Chapter 7 MySQLi– Working with Database and Table
  • 2.
    MySQLi PHP Syntax •MySQLi works very well in combination of various programming languages like PERL, C, C++, JAVA and PHP. • Out of these languages, PHP is the most popular one because of its web application development capabilities. • The PHP functions for use with MySQLi have the following general format: mysqli_function(value,value,...);
  • 3.
    MySQLi PHP Syntax •Example: <?php $variable = mysqli_function(value, [value,...]); if( !$variable ) { Some codes … ; } // Otherwise MySQLi or PHP Statements ?>
  • 4.
    MySQLi Functions Function Description mysqli_connect()Opens a new connection to the MySQL server mysqli_error() Returns the last error description for the most recent function call mysqli_query() Performs a query against the database mysqli_fetch_assoc() Fetches a result row as an associative array mysqli_close() Closes a previously opened database connection
  • 5.
    MySQLi Connection • Beforewe can access data in the MySQL database, we need to be able to connect to the server. • PHP provides mysqli_connect() function to open a database connection. • This function takes five parameters and returns a MySQLi link identifier on success or FALSE on failure.
  • 6.
    MySQLi Connection • Example: <?php $servername= “ "; // localhost $username = “ "; // your own server username $password = “ "; // your own server password // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>
  • 7.
    MySQLi Create/Drop Database •PHP uses mysqli_query function to create or delete a MySQLi database. • This function takes two parameters and returns TRUE on success or FALSE on failure.
  • 8.
    MySQLi Create Database •Example: <?php $dbhost = ‘ '; $dbuser = ‘ '; $dbpass = ‘ '; $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { echo 'Connected failure<br>'; } echo 'Connected successfully<br>'; $sql = "CREATE DATABASE DB1"; if (mysqli_query($conn, $sql)) { echo "Database created successfully"; } else { echo "Error creating database: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 9.
    MySQLi Drop Database •Example: <?php $dbhost = ‘ '; $dbuser = ‘ '; $dbpass = ‘ '; $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { echo 'Connected failure<br>'; } echo 'Connected successfully<br>'; $sql = “DROP DATABASE DB1"; if (mysqli_query($conn, $sql)) { echo “Database deleted successfully"; } else { echo "Error deleting database: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 10.
    MySQLi Select Database •Once you get connection with MySQLi server, it is required to select a particular database to work with. • This is because there may be more than one database available with MySQLi Server.
  • 11.
    MySQLi Select Database •Example: <?php $dbhost = ‘ '; $dbuser = ‘ '; $dbpass = ‘ '; $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { echo 'Connected failure<br>'; } echo 'Connected successfully<br>'; mysqli_select_db( ‘DB1' ); //Write some code here mysqli_close($conn); ?>
  • 12.
    MySQLi Create Table •The table creation command requires: • Name of the table • Names of fields • Definitions for each field • To create new table in any existing database you would need to use PHP function mysqli_query(). • You will pass its second argument with proper SQL command to create a table.
  • 13.
    MySQLi Create Table •Example: <?php // Connection to Database // Select Database $sql = "CREATE TABLE table1( id INT AUTO_INCREMENT,fname VARCHAR(20) NOT NULL,primary key (id))"; if(mysqli_query($conn, $sql)) { echo "Table created successfully"; } else { echo "Table is not created successfully "; } mysqli_close($conn); ?>
  • 14.
    MySQLi Drop Table •It is very easy to drop an existing MySQLi table, but you need to be very careful while deleting any existing table because data lost will not be recovered after deleting a table. • To drop an existing table in any database, you would need to use PHP function mysqli_query(). • You will pass its second argument with proper SQL command to drop a table.
  • 15.
    MySQLi Drop Table •Example: <?php // Connection to Database // Select Database $sql = "DROP TABLE table1"; if(mysqli_query($conn, $sql)) { echo "Table is deleted successfully"; } else { echo "Table is not deleted successfullyn"; } mysqli_close($conn); ?>