SlideShare a Scribd company logo
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);
?>

More Related Content

PDF
4.3 MySQL + PHP
PPTX
PPT
PHP - Intriduction to MySQL And PHP
PPTX
Ch7(publishing my sql data on the web)
PPTX
The hitchhiker's guide to the Webpack - Sara Vieira - Codemotion Amsterdam 2017
PPTX
harry presentation
PDF
MySQL Guide for Beginners
PDF
XQuery in the Cloud
4.3 MySQL + PHP
PHP - Intriduction to MySQL And PHP
Ch7(publishing my sql data on the web)
The hitchhiker's guide to the Webpack - Sara Vieira - Codemotion Amsterdam 2017
harry presentation
MySQL Guide for Beginners
XQuery in the Cloud

What's hot (20)

PDF
Getting Started with Couchbase Ruby
PPT
Memcache
KEY
PDF
XQuery Rocks
PPTX
Using memcache to improve php performance
PPTX
Caching & validating
KEY
Mysqlnd uh
PDF
Not your Grandma's XQuery
PDF
Data models in Angular 1 & 2
PDF
Memcache basics on google app engine
KEY
Introducing Amplify
PDF
Building a theming system with React - Matteo Ronchi - Codemotion Amsterdam 2017
PDF
Cutting Edge Data Processing with PHP & XQuery
PPTX
MySQL Record Operations
KEY
Express Presentation
PDF
Hive jdbc
PDF
Caching basics in PHP
PDF
Introduction to AJAX
PDF
WordPress Café: Using WordPress as a Framework
PDF
Drupal 8 Theme System: The Backend of Frontend
Getting Started with Couchbase Ruby
Memcache
XQuery Rocks
Using memcache to improve php performance
Caching & validating
Mysqlnd uh
Not your Grandma's XQuery
Data models in Angular 1 & 2
Memcache basics on google app engine
Introducing Amplify
Building a theming system with React - Matteo Ronchi - Codemotion Amsterdam 2017
Cutting Edge Data Processing with PHP & XQuery
MySQL Record Operations
Express Presentation
Hive jdbc
Caching basics in PHP
Introduction to AJAX
WordPress Café: Using WordPress as a Framework
Drupal 8 Theme System: The Backend of Frontend
Ad

Similar to Web Application Development using PHP Chapter 7 (20)

PPT
PHP - Getting good with MySQL part II
PPTX
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
PPSX
DIWE - Working with MySQL Databases
PDF
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
PDF
PHP with MySQL
PPTX
lecture 7 - Introduction to MySQL with PHP.pptx
PPT
Synapse india reviews on php and sql
PPTX
Chapter 3.1.pptx
PPTX
PHP DATABASE MANAGEMENT.pptx
PDF
Using php with my sql
PPTX
Connecting_to_Database(MySQL)_in_PHP.pptx
PPTX
Connecting to my sql using PHP
PPTX
Database Connectivity in PHP
PPTX
CHAPTER six DataBase Driven Websites.pptx
PPT
MYSQL - PHP Database Connectivity
PPTX
3-Chapter-Edit.pptx debre tabour university
PPTX
chapter_Seven Database manipulation using php.pptx
PPTX
MySQL with PHP
PPTX
Php Training Workshop by Vtips
PPT
PHP 5 + MySQL 5 = A Perfect 10
PHP - Getting good with MySQL part II
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
DIWE - Working with MySQL Databases
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
PHP with MySQL
lecture 7 - Introduction to MySQL with PHP.pptx
Synapse india reviews on php and sql
Chapter 3.1.pptx
PHP DATABASE MANAGEMENT.pptx
Using php with my sql
Connecting_to_Database(MySQL)_in_PHP.pptx
Connecting to my sql using PHP
Database Connectivity in PHP
CHAPTER six DataBase Driven Websites.pptx
MYSQL - PHP Database Connectivity
3-Chapter-Edit.pptx debre tabour university
chapter_Seven Database manipulation using php.pptx
MySQL with PHP
Php Training Workshop by Vtips
PHP 5 + MySQL 5 = A Perfect 10
Ad

More from Mohd Harris Ahmad Jaal (20)

PPT
Fundamentals of Programming Chapter 7
PPT
Fundamentals of Programming Chapter 6
PPT
Fundamentals of Programming Chapter 4
PPT
Fundamentals of Programming Chapter 3
PPT
Fundamentals of Programming Chapter 2
PPT
Fundamentals of Programming Chapter 1
PPT
Fundamentals of Programming Chapter 5
PPTX
Web Application Development using PHP Chapter 8
PPTX
Web Application Development using PHP Chapter 6
PPTX
Web Application Development using PHP Chapter 5
PPTX
Web Application Development using PHP Chapter 4
PPTX
Web Application Development using PHP Chapter 3
PPTX
Web Application Development using PHP Chapter 2
PPTX
Web Application Development using PHP Chapter 1
PPT
Fundamentals of Computing Chapter 10
PPT
Fundamentals of Computing Chapter 9
PPT
Fundamentals of Computing Chapter 8
PPT
Fundamentals of Computing Chapter 7
PPT
Fundamentals of Computing Chapter 6
PPT
Fundamentals of Computing Chapter 5
Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 5
Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 1
Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 5

Recently uploaded (20)

PDF
From loneliness to social connection charting
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
From loneliness to social connection charting
Revamp in MTO Odoo 18 Inventory - Odoo Slides
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Structure & Organelles in detailed.
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
O7-L3 Supply Chain Operations - ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Introduction and Scope of Bichemistry.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Insiders guide to clinical Medicine.pdf
Business Ethics Teaching Materials for college
Cardiovascular Pharmacology for pharmacy students.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?

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 • 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.
  • 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); ?>