SlideShare a Scribd company logo
Database
PHP  support variety of database management systems, including : - MySQL - PostgreSQL - Oracle - Microsoft Access  MySQL very fast very reliable very feature-rich open-source RDBMS Every MySQL database is composed of : one or more  tables .  These tables, which: structure data into rows and columns,  are what lend organization to the data.
CREATE DATABASE testdb;  CREATE TABLE `symbols`  (      `id` int(11) NOT NULL auto_increment,      `country` varchar(255) NOT NULL default '',      `animal` varchar(255) NOT NULL default '',      PRIMARY KEY  (`id`)  )  INSERT INTO `symbols` VALUES (1, 'America', 'eagle');  INSERT INTO `symbols` VALUES (2, 'China', 'dragon');  INSERT INTO `symbols` VALUES (3, 'England', 'lion');  INSERT INTO `symbols` VALUES (4, 'India', 'tiger');  INSERT INTO `symbols` VALUES (5, 'Australia', 'kangaroo');  INSERT INTO `symbols` VALUES (6, 'Norway', 'elk');  FROM My SQL
Retrieve data from My Sql Database in PHP <?php  // set database server access variables:  $host  =  &quot;localhost&quot; ;  $user  =  &quot;test&quot; ;  $pass  =  &quot;test&quot; ;  $db  =  &quot;testdb&quot; ;  // open connection  $connection  =  mysql_connect ( $host ,  $user ,  $pass ) or die ( &quot;Unable to connect!&quot; );  // select database  mysql_select_db ( $db ) or die ( &quot;Unable to select database!&quot; );  // create query  $query  =  &quot;SELECT * FROM symbols&quot; ;  // execute query  $result  =  mysql_query ( $query ) or die ( &quot;Error in query: $query. &quot; . mysql_error ());  Cont …
// see if any rows were returned  if ( mysql_num_rows ( $result ) >  0 ) {       // yes      // print them one after another       echo  &quot;<table cellpadding=10 border=1>&quot; ;      while( $row  =  mysql_fetch_row ( $result )) {          echo  &quot;<tr>&quot; ;          echo  &quot;<td>&quot; . $row [ 0 ]. &quot;</td>&quot; ;          echo  &quot;<td>&quot;  .  $row [ 1 ]. &quot;</td>&quot; ;          echo  &quot;<td>&quot; . $row [ 2 ]. &quot;</td>&quot; ;          echo  &quot;</tr>&quot; ;      }      echo  &quot;</table>&quot; ;  }  else {       // no      // print status message       echo  &quot;No rows found!&quot; ;  }  // free result set memory  mysql_free_result ( $result );  // close connection  mysql_close ( $connection );  ?>
OUTPUT
mysql_fetch_array()  Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.  mysql_fetch_row()  returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc()  returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.  mysql_fetch_assoc()  is equivalent to calling  mysql_fetch_array()  with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.  mysql_fetch_object()  returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.
mysql_fetch_array() <?php  $host  =  &quot;localhost&quot; ;  $user  =  &quot;root&quot; ;  $pass  =  &quot;guessme&quot; ;  $db  =  &quot;testdb&quot; ;  $connection  =  mysql_connect ( $host ,  $user ,  $pass ) or die ( &quot;Unable to connect!&quot; );  // get database list  $query  =  &quot;SHOW DATABASES&quot; ;  $result  =  mysql_query ( $query ) or die ( &quot;Error in query: $query. &quot; . mysql_error ());  echo  &quot;<ul>&quot; ;  while ( $row  =  mysql_fetch_array ( $result )) {      echo  &quot;<li>&quot; . $row [ 0 ];       // for each database, get table list and print       $query2  =  &quot;SHOW TABLES FROM &quot; . $row [ 0 ];       $result2  =  mysql_query ( $query2 ) or die ( &quot;Error in query: $query2. &quot; . mysql_error ());      echo  &quot;<ul>&quot; ;      while ( $row2  =  mysql_fetch_array ( $result2 )) {          echo  &quot;<li>&quot; . $row2 [ 0 ];  }      echo  &quot;</ul>&quot; ;  }  echo  &quot;</ul>&quot; ;  // get version and host information  echo  &quot;Client version: &quot; . mysql_get_client_info (). &quot;<br />&quot; ;  echo  &quot;Server version: &quot; . mysql_get_server_info (). &quot;<br />&quot; ;  echo  &quot;Protocol version: &quot; . mysql_get_proto_info (). &quot;<br />&quot; ;  echo  &quot;Host: &quot; . mysql_get_host_info (). &quot;<br />&quot; ;  // get server status  $status  =  mysql_stat ();  echo  $status ;  // close connection  mysql_close ( $connection );  ?>
OUTPUT
mysql_fetch_row() // see if any rows were returned  if ( mysql_num_rows ( $result ) >  0 ) {        // yes       // print them one after another        echo  &quot;<table cellpadding=10 border=1>&quot; ;       while(list( $id ,  $country ,  $animal )  =  mysql_fetch_row ( $result )) {            echo  &quot;<tr>&quot; ;            echo  &quot;<td>$id</td>&quot; ;            echo  &quot;<td>$country</td>&quot; ;            echo  &quot;<td>$animal</td>&quot; ;            echo  &quot;</tr>&quot; ;       }       echo  &quot;</table>&quot; ;  }  else {        // no       // print status message        echo  &quot;No rows found!&quot; ;  }   OUTPUT
mysql_fetch_assoc() // see if any rows were returned  if ( mysql_num_rows ( $result ) >  0 ) {       // yes      // print them one after another       echo  &quot;<table cellpadding=10 border=1>&quot; ;      while( $row  =  mysql_fetch_assoc ( $result )) {          echo  &quot;<tr>&quot; ;          echo  &quot;<td>&quot; . $row [ 'id' ]. &quot;</td>&quot; ;          echo  &quot;<td>&quot; . $row [ 'country' ]. &quot;</td>&quot; ;          echo  &quot;<td>&quot; . $row [ 'animal' ]. &quot;</td>&quot; ;          echo  &quot;</tr>&quot; ;      }      echo  &quot;</table>&quot; ;  }  else {       // no      // print status message       echo  &quot;No rows found!&quot; ;  }   OUTPUT
mysql_fetch_object() // see if any rows were returned  if ( mysql_num_rows ( $result ) >  0 ) {       // yes      // print them one after another       echo  &quot;<table cellpadding=10 border=1>&quot; ;      while( $row   =  mysql_fetch_object ( $result )) {          echo  &quot;<tr>&quot; ;          echo  &quot;<td>&quot; . $row -> id . &quot;</td>&quot; ;          echo  &quot;<td>&quot; . $row -> country . &quot;</td>&quot; ;          echo  &quot;<td>&quot; . $row -> animal . &quot;</td>&quot; ;          echo  &quot;</tr>&quot; ;      }      echo  &quot;</table>&quot; ;  }  else {       // no      // print status message       echo  &quot;No rows found!&quot; ;  }   OUTPUT
Form application in php and mysql database <html>  <head>  <basefont face=&quot;Arial&quot;>  </head>  <body>  <?php  if (!isset( $_POST [ 'submit' ])) {  // form not submitted  ?>      <form action=&quot; <?=$_SERVER [ 'PHP_SELF' ] ?> &quot; method=&quot;post&quot;>      Country: <input type=&quot;text&quot; name=&quot;country&quot;>      National animal: <input type=&quot;text&quot; name=&quot;animal&quot;>      <input type=&quot;submit&quot; name=&quot;submit&quot;>      </form>   Cont …
<?php  }  else {  // form submitted  // set server access variables       $host  =  &quot;localhost&quot; ;       $user  =  &quot;test&quot; ;       $pass  =  &quot;test&quot; ;       $db  =  &quot;testdb&quot; ;        // get form input      // check to make sure it's all there      // escape input values for greater safety       $country  = empty( $_POST [ 'country' ]) ?  die ( &quot;ERROR: Enter a country&quot; ) :  mysql_escape_string ( $_POST [ 'country' ]);       $animal  = empty( $_POST [ 'animal' ]) ?  die ( &quot;ERROR: Enter an animal&quot; ) :  mysql_escape_string ( $_POST [ 'animal' ]);       // open connection       $connection  =  mysql_connect ( $host ,  $user ,  $pass ) or die ( &quot;Unable to connect!&quot; );   Cont …
// select database       mysql_select_db ( $db ) or die ( &quot;Unable to select database!&quot; );             // create query       $query  =  &quot;INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')&quot; ;             // execute query       $result  =  mysql_query ( $query ) or die ( &quot;Error in query: $query. &quot; . mysql_error ());             // print message with ID of inserted record       echo  &quot;New record inserted with ID &quot; . mysql_insert_id ();             // close connection       mysql_close ( $connection );  }  ?>  </body>  </html>
OUTPUT
mysqli library <html>  <head>  <basefont face=&quot;Arial&quot;>  </head>  <body>  <?php  // set server access variables  $host  =  &quot;localhost&quot; ;  $user  =  &quot;test&quot; ;  $pass  =  &quot;test&quot; ;  $db  =  &quot;testdb&quot; ;  // create mysqli object  // open connection  $mysqli  = new  mysqli ( $host ,  $user ,  $pass ,  $db );  // check for connection errors  if ( mysqli_connect_errno ()) {      die( &quot;Unable to connect!&quot; );  }  // create query  $query  =  &quot;SELECT * FROM symbols&quot; ;
if ( $result  =  $mysqli -> query ( $query )) {       // see if any rows were returned       if ( $result -> num_rows  >  0 ) {           // yes          // print them one after another           echo  &quot;<table cellpadding=10 border=1>&quot; ;          while( $row  =  $result -> fetch_array ()) {              echo  &quot;<tr>&quot; ;              echo  &quot;<td>&quot; . $row [ 0 ]. &quot;</td>&quot; ;              echo  &quot;<td>&quot; . $row [ 1 ]. &quot;</td>&quot; ;              echo  &quot;<td>&quot; . $row [ 2 ]. &quot;</td>&quot; ;              echo  &quot;</tr>&quot; ;          }          echo  &quot;</table>&quot; ;      }      else {           // no          // print status message           echo  &quot;No rows found!&quot; ;   }       // free result set memory       $result -> close ();  }  else {       // print error message       echo  &quot;Error in query: $query. &quot; . $mysqli -> error ;  }  // close connection  $mysqli -> close ();  ?>  </body>  </html>
OUTPUT
Delete record from mysqli library <?php  // set server access variables  $host  =  &quot;localhost&quot; ;  $user  =  &quot;test&quot; ;  $pass  =  &quot;test&quot; ;  $db  =  &quot;testdb&quot; ;  // create mysqli object  // open connection  $mysqli  = new  mysqli ( $host ,  $user ,  $pass ,  $db );  // check for connection errors  if ( mysqli_connect_errno ()) {      die( &quot;Unable to connect!&quot; );  }  // if id provided, then delete that record  if (isset( $_GET [ 'id' ])) {  // create query to delete record       $query  =  &quot;DELETE FROM symbols WHERE id = &quot; . $_GET [ 'id' ];   Cont …
// execute query       if ( $mysqli -> query ( $query )) {       // print number of affected rows       echo  $mysqli -> affected_rows . &quot; row(s) affected&quot; ;      }      else {       // print error message       echo  &quot;Error in query: $query. &quot; . $mysqli -> error ;      }  }  // query to get records  $query  =  &quot;SELECT * FROM symbols&quot; ;   Cont …
// execute query  if ( $result  =  $mysqli -> query ( $query )) {       // see if any rows were returned       if ( $result -> num_rows  >  0 ) {           // yes          // print them one after another           echo  &quot;<table cellpadding=10 border=1>&quot; ;          while( $row  =  $result -> fetch_array ()) {              echo  &quot;<tr>&quot; ;              echo  &quot;<td>&quot; . $row [ 0 ]. &quot;</td>&quot; ;              echo  &quot;<td>&quot; . $row [ 1 ]. &quot;</td>&quot; ;              echo  &quot;<td>&quot; . $row [ 2 ]. &quot;</td>&quot; ;              echo  &quot;<td><a href=&quot; . $_SERVER [ 'PHP_SELF' ]. &quot;?id=&quot; . $row [ 0 ]. &quot;>Delete</a></td>&quot; ;              echo  &quot;</tr>&quot; ;          }      }       // free result set memory       $result -> close ();  }  else {       // print error message       echo  &quot;Error in query: $query. &quot; . $mysqli -> error ;  }  // close connection  $mysqli -> close ();  ?>
OUTPUT
THE END

More Related Content

PPT
Php Sq Lite
PPT
Ubi comp27nov04
PPTX
SQL Injection Part 2
PPTX
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
PPT
JQuery Basics
PDF
[PL] Jak nie zostać "programistą" PHP?
PPTX
Let's write secure Drupal code! - Drupal Camp Poland 2019
PPTX
Let's write secure Drupal code! DUG Belgium - 08/08/2019
Php Sq Lite
Ubi comp27nov04
SQL Injection Part 2
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
JQuery Basics
[PL] Jak nie zostać "programistą" PHP?
Let's write secure Drupal code! - Drupal Camp Poland 2019
Let's write secure Drupal code! DUG Belgium - 08/08/2019

What's hot (20)

PPTX
Building Your First Widget
PPT
Propel sfugmd
PPTX
Let's write secure drupal code! - Drupal Camp Pannonia 2019
PPTX
Views notwithstanding
PDF
Current state-of-php
PDF
Get into the FLOW with Extbase
PDF
Play, Slick, play2-authの間で討死
PPTX
CSS: A Slippery Slope to the Backend
ODP
Concern of Web Application Security
PDF
Pagination in PHP
PDF
Sorting arrays in PHP
PDF
Your code sucks, let's fix it! - php|tek13
PDF
Add edit delete in Codeigniter in PHP
PDF
Country State City Dropdown in PHP
TXT
Wsomdp
PPTX
PHP Functions & Arrays
ODP
Intro to #memtech PHP 2011-12-05
PDF
2014 database - course 2 - php
PDF
Apache Solr Search Mastery
PPT
Php Basic Security
Building Your First Widget
Propel sfugmd
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Views notwithstanding
Current state-of-php
Get into the FLOW with Extbase
Play, Slick, play2-authの間で討死
CSS: A Slippery Slope to the Backend
Concern of Web Application Security
Pagination in PHP
Sorting arrays in PHP
Your code sucks, let's fix it! - php|tek13
Add edit delete in Codeigniter in PHP
Country State City Dropdown in PHP
Wsomdp
PHP Functions & Arrays
Intro to #memtech PHP 2011-12-05
2014 database - course 2 - php
Apache Solr Search Mastery
Php Basic Security
Ad

Viewers also liked (18)

PPTX
Introduction to Computer Network
ZIP
Week3 Lecture Database Design
PPT
Advance Database Management Systems -Object Oriented Principles In Database
PPTX
Fundamentals of Database Design
PPT
Database design
PPT
Database design
PPTX
Importance of database design (1)
PPT
Database Design Process
PPTX
Advanced DBMS presentation
PPT
ADVANCE DATABASE MANAGEMENT SYSTEM CONCEPTS & ARCHITECTURE by vikas jagtap
PPTX
Entity relationship diagram - Concept on normalization
PPTX
Learn Database Design with MySQL - Chapter 6 - Database design process
PPT
Sql ppt
PPTX
Introduction to database
PDF
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
PPT
Basic DBMS ppt
PPTX
Dbms slides
Introduction to Computer Network
Week3 Lecture Database Design
Advance Database Management Systems -Object Oriented Principles In Database
Fundamentals of Database Design
Database design
Database design
Importance of database design (1)
Database Design Process
Advanced DBMS presentation
ADVANCE DATABASE MANAGEMENT SYSTEM CONCEPTS & ARCHITECTURE by vikas jagtap
Entity relationship diagram - Concept on normalization
Learn Database Design with MySQL - Chapter 6 - Database design process
Sql ppt
Introduction to database
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Basic DBMS ppt
Dbms slides
Ad

Similar to Php My Sql (20)

PPT
Database presentation
PPT
Php MySql For Beginners
PPT
SQL -PHP Tutorial
PDF
PHP and Rich Internet Applications
PPT
PHP 5 + MySQL 5 = A Perfect 10
PPT
P H P Part I I, By Kian
PPT
Download It
PPTX
working with PHP & DB's
PPTX
Practical MySQL.pptx
PPT
Migrating from PHP 4 to PHP 5
PPTX
Mysql
PPT
Open Source Package PHP & MySQL
PPT
Open Source Package Php Mysql 1228203701094763 9
PPT
Short Intro to PHP and MySQL
PDF
PHP with MySQL
PDF
lab56_db
PDF
lab56_db
PPT
Diva10
Database presentation
Php MySql For Beginners
SQL -PHP Tutorial
PHP and Rich Internet Applications
PHP 5 + MySQL 5 = A Perfect 10
P H P Part I I, By Kian
Download It
working with PHP & DB's
Practical MySQL.pptx
Migrating from PHP 4 to PHP 5
Mysql
Open Source Package PHP & MySQL
Open Source Package Php Mysql 1228203701094763 9
Short Intro to PHP and MySQL
PHP with MySQL
lab56_db
lab56_db
Diva10

More from mussawir20 (20)

PPT
Php Operators N Controllers
PPT
Php Calling Operators
PPT
Php Simple Xml
PPT
Php String And Regular Expressions
PPT
Php Sessoins N Cookies
PPT
Php Rss
PPT
Php Reusing Code And Writing Functions
PPT
Php Oop
PPT
Php File Operations
PPT
Php Error Handling
PPT
Php Crash Course
PPT
Php Using Arrays
PPT
Javascript Oop
PPT
PPT
Javascript
PPT
Object Range
PPT
Prototype Utility Methods(1)
PPT
PPT
Prototype js
PPT
Template
Php Operators N Controllers
Php Calling Operators
Php Simple Xml
Php String And Regular Expressions
Php Sessoins N Cookies
Php Rss
Php Reusing Code And Writing Functions
Php Oop
Php File Operations
Php Error Handling
Php Crash Course
Php Using Arrays
Javascript Oop
Javascript
Object Range
Prototype Utility Methods(1)
Prototype js
Template

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Electronic commerce courselecture one. Pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
A Presentation on Artificial Intelligence
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Electronic commerce courselecture one. Pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
A Presentation on Artificial Intelligence
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
MYSQL Presentation for SQL database connectivity
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
Unlocking AI with Model Context Protocol (MCP)

Php My Sql

  • 2. PHP support variety of database management systems, including : - MySQL - PostgreSQL - Oracle - Microsoft Access MySQL very fast very reliable very feature-rich open-source RDBMS Every MySQL database is composed of : one or more tables . These tables, which: structure data into rows and columns, are what lend organization to the data.
  • 3. CREATE DATABASE testdb; CREATE TABLE `symbols` (     `id` int(11) NOT NULL auto_increment,     `country` varchar(255) NOT NULL default '',     `animal` varchar(255) NOT NULL default '',     PRIMARY KEY  (`id`) ) INSERT INTO `symbols` VALUES (1, 'America', 'eagle'); INSERT INTO `symbols` VALUES (2, 'China', 'dragon'); INSERT INTO `symbols` VALUES (3, 'England', 'lion'); INSERT INTO `symbols` VALUES (4, 'India', 'tiger'); INSERT INTO `symbols` VALUES (5, 'Australia', 'kangaroo'); INSERT INTO `symbols` VALUES (6, 'Norway', 'elk'); FROM My SQL
  • 4. Retrieve data from My Sql Database in PHP <?php // set database server access variables: $host = &quot;localhost&quot; ; $user = &quot;test&quot; ; $pass = &quot;test&quot; ; $db = &quot;testdb&quot; ; // open connection $connection = mysql_connect ( $host , $user , $pass ) or die ( &quot;Unable to connect!&quot; ); // select database mysql_select_db ( $db ) or die ( &quot;Unable to select database!&quot; ); // create query $query = &quot;SELECT * FROM symbols&quot; ; // execute query $result = mysql_query ( $query ) or die ( &quot;Error in query: $query. &quot; . mysql_error ()); Cont …
  • 5. // see if any rows were returned if ( mysql_num_rows ( $result ) > 0 ) {      // yes     // print them one after another      echo &quot;<table cellpadding=10 border=1>&quot; ;     while( $row = mysql_fetch_row ( $result )) {         echo &quot;<tr>&quot; ;         echo &quot;<td>&quot; . $row [ 0 ]. &quot;</td>&quot; ;         echo &quot;<td>&quot; . $row [ 1 ]. &quot;</td>&quot; ;         echo &quot;<td>&quot; . $row [ 2 ]. &quot;</td>&quot; ;         echo &quot;</tr>&quot; ;     }     echo &quot;</table>&quot; ; } else {      // no     // print status message      echo &quot;No rows found!&quot; ; } // free result set memory mysql_free_result ( $result ); // close connection mysql_close ( $connection ); ?>
  • 7. mysql_fetch_array() Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_row() returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. mysql_fetch_object() returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.
  • 8. mysql_fetch_array() <?php $host = &quot;localhost&quot; ; $user = &quot;root&quot; ; $pass = &quot;guessme&quot; ; $db = &quot;testdb&quot; ; $connection = mysql_connect ( $host , $user , $pass ) or die ( &quot;Unable to connect!&quot; ); // get database list $query = &quot;SHOW DATABASES&quot; ; $result = mysql_query ( $query ) or die ( &quot;Error in query: $query. &quot; . mysql_error ()); echo &quot;<ul>&quot; ; while ( $row = mysql_fetch_array ( $result )) {     echo &quot;<li>&quot; . $row [ 0 ];      // for each database, get table list and print      $query2 = &quot;SHOW TABLES FROM &quot; . $row [ 0 ];      $result2 = mysql_query ( $query2 ) or die ( &quot;Error in query: $query2. &quot; . mysql_error ());     echo &quot;<ul>&quot; ;     while ( $row2 = mysql_fetch_array ( $result2 )) {         echo &quot;<li>&quot; . $row2 [ 0 ]; }     echo &quot;</ul>&quot; ; } echo &quot;</ul>&quot; ; // get version and host information echo &quot;Client version: &quot; . mysql_get_client_info (). &quot;<br />&quot; ; echo &quot;Server version: &quot; . mysql_get_server_info (). &quot;<br />&quot; ; echo &quot;Protocol version: &quot; . mysql_get_proto_info (). &quot;<br />&quot; ; echo &quot;Host: &quot; . mysql_get_host_info (). &quot;<br />&quot; ; // get server status $status = mysql_stat (); echo $status ; // close connection mysql_close ( $connection ); ?>
  • 10. mysql_fetch_row() // see if any rows were returned if ( mysql_num_rows ( $result ) > 0 ) {       // yes      // print them one after another       echo &quot;<table cellpadding=10 border=1>&quot; ;      while(list( $id , $country , $animal )  = mysql_fetch_row ( $result )) {           echo &quot;<tr>&quot; ;           echo &quot;<td>$id</td>&quot; ;           echo &quot;<td>$country</td>&quot; ;           echo &quot;<td>$animal</td>&quot; ;           echo &quot;</tr>&quot; ;      }      echo &quot;</table>&quot; ; } else {       // no      // print status message       echo &quot;No rows found!&quot; ; } OUTPUT
  • 11. mysql_fetch_assoc() // see if any rows were returned if ( mysql_num_rows ( $result ) > 0 ) {      // yes     // print them one after another      echo &quot;<table cellpadding=10 border=1>&quot; ;     while( $row = mysql_fetch_assoc ( $result )) {         echo &quot;<tr>&quot; ;         echo &quot;<td>&quot; . $row [ 'id' ]. &quot;</td>&quot; ;         echo &quot;<td>&quot; . $row [ 'country' ]. &quot;</td>&quot; ;         echo &quot;<td>&quot; . $row [ 'animal' ]. &quot;</td>&quot; ;         echo &quot;</tr>&quot; ;     }     echo &quot;</table>&quot; ; } else {      // no     // print status message      echo &quot;No rows found!&quot; ; } OUTPUT
  • 12. mysql_fetch_object() // see if any rows were returned if ( mysql_num_rows ( $result ) > 0 ) {      // yes     // print them one after another      echo &quot;<table cellpadding=10 border=1>&quot; ;     while( $row   = mysql_fetch_object ( $result )) {         echo &quot;<tr>&quot; ;         echo &quot;<td>&quot; . $row -> id . &quot;</td>&quot; ;         echo &quot;<td>&quot; . $row -> country . &quot;</td>&quot; ;         echo &quot;<td>&quot; . $row -> animal . &quot;</td>&quot; ;         echo &quot;</tr>&quot; ;     }     echo &quot;</table>&quot; ; } else {      // no     // print status message      echo &quot;No rows found!&quot; ; } OUTPUT
  • 13. Form application in php and mysql database <html> <head> <basefont face=&quot;Arial&quot;> </head> <body> <?php if (!isset( $_POST [ 'submit' ])) { // form not submitted ?>     <form action=&quot; <?=$_SERVER [ 'PHP_SELF' ] ?> &quot; method=&quot;post&quot;>     Country: <input type=&quot;text&quot; name=&quot;country&quot;>     National animal: <input type=&quot;text&quot; name=&quot;animal&quot;>     <input type=&quot;submit&quot; name=&quot;submit&quot;>     </form> Cont …
  • 14. <?php } else { // form submitted // set server access variables      $host = &quot;localhost&quot; ;      $user = &quot;test&quot; ;      $pass = &quot;test&quot; ;      $db = &quot;testdb&quot; ;      // get form input     // check to make sure it's all there     // escape input values for greater safety      $country = empty( $_POST [ 'country' ]) ? die ( &quot;ERROR: Enter a country&quot; ) : mysql_escape_string ( $_POST [ 'country' ]);      $animal = empty( $_POST [ 'animal' ]) ? die ( &quot;ERROR: Enter an animal&quot; ) : mysql_escape_string ( $_POST [ 'animal' ]);      // open connection      $connection = mysql_connect ( $host , $user , $pass ) or die ( &quot;Unable to connect!&quot; ); Cont …
  • 15. // select database      mysql_select_db ( $db ) or die ( &quot;Unable to select database!&quot; );           // create query      $query = &quot;INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')&quot; ;           // execute query      $result = mysql_query ( $query ) or die ( &quot;Error in query: $query. &quot; . mysql_error ());           // print message with ID of inserted record      echo &quot;New record inserted with ID &quot; . mysql_insert_id ();           // close connection      mysql_close ( $connection ); } ?> </body> </html>
  • 17. mysqli library <html> <head> <basefont face=&quot;Arial&quot;> </head> <body> <?php // set server access variables $host = &quot;localhost&quot; ; $user = &quot;test&quot; ; $pass = &quot;test&quot; ; $db = &quot;testdb&quot; ; // create mysqli object // open connection $mysqli = new mysqli ( $host , $user , $pass , $db ); // check for connection errors if ( mysqli_connect_errno ()) {     die( &quot;Unable to connect!&quot; ); } // create query $query = &quot;SELECT * FROM symbols&quot; ;
  • 18. if ( $result = $mysqli -> query ( $query )) {      // see if any rows were returned      if ( $result -> num_rows > 0 ) {          // yes         // print them one after another          echo &quot;<table cellpadding=10 border=1>&quot; ;         while( $row = $result -> fetch_array ()) {             echo &quot;<tr>&quot; ;             echo &quot;<td>&quot; . $row [ 0 ]. &quot;</td>&quot; ;             echo &quot;<td>&quot; . $row [ 1 ]. &quot;</td>&quot; ;             echo &quot;<td>&quot; . $row [ 2 ]. &quot;</td>&quot; ;             echo &quot;</tr>&quot; ;         }         echo &quot;</table>&quot; ;     }     else {          // no         // print status message          echo &quot;No rows found!&quot; ;  }      // free result set memory      $result -> close (); } else {      // print error message      echo &quot;Error in query: $query. &quot; . $mysqli -> error ; } // close connection $mysqli -> close (); ?> </body> </html>
  • 20. Delete record from mysqli library <?php // set server access variables $host = &quot;localhost&quot; ; $user = &quot;test&quot; ; $pass = &quot;test&quot; ; $db = &quot;testdb&quot; ; // create mysqli object // open connection $mysqli = new mysqli ( $host , $user , $pass , $db ); // check for connection errors if ( mysqli_connect_errno ()) {     die( &quot;Unable to connect!&quot; ); } // if id provided, then delete that record if (isset( $_GET [ 'id' ])) { // create query to delete record      $query = &quot;DELETE FROM symbols WHERE id = &quot; . $_GET [ 'id' ]; Cont …
  • 21. // execute query      if ( $mysqli -> query ( $query )) {      // print number of affected rows      echo $mysqli -> affected_rows . &quot; row(s) affected&quot; ;     }     else {      // print error message      echo &quot;Error in query: $query. &quot; . $mysqli -> error ;     } } // query to get records $query = &quot;SELECT * FROM symbols&quot; ; Cont …
  • 22. // execute query if ( $result = $mysqli -> query ( $query )) {      // see if any rows were returned      if ( $result -> num_rows > 0 ) {          // yes         // print them one after another          echo &quot;<table cellpadding=10 border=1>&quot; ;         while( $row = $result -> fetch_array ()) {             echo &quot;<tr>&quot; ;             echo &quot;<td>&quot; . $row [ 0 ]. &quot;</td>&quot; ;             echo &quot;<td>&quot; . $row [ 1 ]. &quot;</td>&quot; ;             echo &quot;<td>&quot; . $row [ 2 ]. &quot;</td>&quot; ;             echo &quot;<td><a href=&quot; . $_SERVER [ 'PHP_SELF' ]. &quot;?id=&quot; . $row [ 0 ]. &quot;>Delete</a></td>&quot; ;             echo &quot;</tr>&quot; ;         }     }      // free result set memory      $result -> close (); } else {      // print error message      echo &quot;Error in query: $query. &quot; . $mysqli -> error ; } // close connection $mysqli -> close (); ?>