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

What's hot (20)

PPTX
Building Your First Widget
Chris Wilcoxson
 
PPT
Propel sfugmd
iKlaus
 
PPTX
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Balázs Tatár
 
PPTX
Views notwithstanding
Srikanth Bangalore
 
PDF
Current state-of-php
Richard McIntyre
 
PDF
Get into the FLOW with Extbase
Jochen Rau
 
PDF
Play, Slick, play2-authの間で討死
Kiwamu Okabe
 
PPTX
CSS: A Slippery Slope to the Backend
FITC
 
ODP
Concern of Web Application Security
Mahmud Ahsan
 
PDF
Pagination in PHP
Vineet Kumar Saini
 
PDF
Sorting arrays in PHP
Vineet Kumar Saini
 
PDF
Your code sucks, let's fix it! - php|tek13
Rafael Dohms
 
PDF
Add edit delete in Codeigniter in PHP
Vineet Kumar Saini
 
PDF
Country State City Dropdown in PHP
Vineet Kumar Saini
 
TXT
Wsomdp
riahialae
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
ODP
Intro to #memtech PHP 2011-12-05
Jeremy Kendall
 
PDF
2014 database - course 2 - php
Hung-yu Lin
 
PDF
Apache Solr Search Mastery
Acquia
 
PPT
Php Basic Security
mussawir20
 
Building Your First Widget
Chris Wilcoxson
 
Propel sfugmd
iKlaus
 
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Balázs Tatár
 
Views notwithstanding
Srikanth Bangalore
 
Current state-of-php
Richard McIntyre
 
Get into the FLOW with Extbase
Jochen Rau
 
Play, Slick, play2-authの間で討死
Kiwamu Okabe
 
CSS: A Slippery Slope to the Backend
FITC
 
Concern of Web Application Security
Mahmud Ahsan
 
Pagination in PHP
Vineet Kumar Saini
 
Sorting arrays in PHP
Vineet Kumar Saini
 
Your code sucks, let's fix it! - php|tek13
Rafael Dohms
 
Add edit delete in Codeigniter in PHP
Vineet Kumar Saini
 
Country State City Dropdown in PHP
Vineet Kumar Saini
 
Wsomdp
riahialae
 
PHP Functions & Arrays
Henry Osborne
 
Intro to #memtech PHP 2011-12-05
Jeremy Kendall
 
2014 database - course 2 - php
Hung-yu Lin
 
Apache Solr Search Mastery
Acquia
 
Php Basic Security
mussawir20
 

Viewers also liked (18)

PPTX
Introduction to Computer Network
Adetula Bunmi
 
ZIP
Week3 Lecture Database Design
Kevin Element
 
PPT
Advance Database Management Systems -Object Oriented Principles In Database
Sonali Parab
 
PPTX
Fundamentals of Database Design
Information Technology
 
PPT
Database design
Jennifer Polack
 
PPT
Database design
FLYMAN TECHNOLOGY LIMITED
 
PPTX
Importance of database design (1)
yhen06
 
PPT
Database Design Process
mussawir20
 
PPTX
Advanced DBMS presentation
Hindustan Petroleum
 
PPT
ADVANCE DATABASE MANAGEMENT SYSTEM CONCEPTS & ARCHITECTURE by vikas jagtap
Vikas Jagtap
 
PPTX
Entity relationship diagram - Concept on normalization
Satya Pal
 
PPTX
Learn Database Design with MySQL - Chapter 6 - Database design process
Eduonix Learning Solutions
 
PPT
Sql ppt
Anuja Lad
 
PPTX
Introduction to database
Pongsakorn U-chupala
 
PDF
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Beat Signer
 
PPT
Basic DBMS ppt
dangwalrajendra888
 
PPTX
Dbms slides
rahulrathore725
 
Introduction to Computer Network
Adetula Bunmi
 
Week3 Lecture Database Design
Kevin Element
 
Advance Database Management Systems -Object Oriented Principles In Database
Sonali Parab
 
Fundamentals of Database Design
Information Technology
 
Database design
Jennifer Polack
 
Database design
FLYMAN TECHNOLOGY LIMITED
 
Importance of database design (1)
yhen06
 
Database Design Process
mussawir20
 
Advanced DBMS presentation
Hindustan Petroleum
 
ADVANCE DATABASE MANAGEMENT SYSTEM CONCEPTS & ARCHITECTURE by vikas jagtap
Vikas Jagtap
 
Entity relationship diagram - Concept on normalization
Satya Pal
 
Learn Database Design with MySQL - Chapter 6 - Database design process
Eduonix Learning Solutions
 
Sql ppt
Anuja Lad
 
Introduction to database
Pongsakorn U-chupala
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Beat Signer
 
Basic DBMS ppt
dangwalrajendra888
 
Dbms slides
rahulrathore725
 
Ad

Similar to Php My Sql (20)

PPT
Database presentation
webhostingguy
 
PPT
Php MySql For Beginners
Priti Solanki
 
PPT
SQL -PHP Tutorial
Information Technology
 
PDF
PHP and Rich Internet Applications
elliando dias
 
PPT
PHP 5 + MySQL 5 = A Perfect 10
Adam Trachtenberg
 
PPT
P H P Part I I, By Kian
phelios
 
PPT
Download It
webhostingguy
 
PPTX
working with PHP & DB's
Hi-Tech College
 
PPTX
Practical MySQL.pptx
HussainUsman4
 
PPT
Migrating from PHP 4 to PHP 5
John Coggeshall
 
PPTX
Mysql
lotlot
 
PPT
Open Source Package Php Mysql 1228203701094763 9
isadorta
 
PPT
Open Source Package PHP & MySQL
kalaisai
 
PPT
Short Intro to PHP and MySQL
Jussi Pohjolainen
 
PDF
PHP with MySQL
wahidullah mudaser
 
PDF
lab56_db
tutorialsruby
 
PDF
lab56_db
tutorialsruby
 
PPT
Diva10
diva23
 
Database presentation
webhostingguy
 
Php MySql For Beginners
Priti Solanki
 
SQL -PHP Tutorial
Information Technology
 
PHP and Rich Internet Applications
elliando dias
 
PHP 5 + MySQL 5 = A Perfect 10
Adam Trachtenberg
 
P H P Part I I, By Kian
phelios
 
Download It
webhostingguy
 
working with PHP & DB's
Hi-Tech College
 
Practical MySQL.pptx
HussainUsman4
 
Migrating from PHP 4 to PHP 5
John Coggeshall
 
Mysql
lotlot
 
Open Source Package Php Mysql 1228203701094763 9
isadorta
 
Open Source Package PHP & MySQL
kalaisai
 
Short Intro to PHP and MySQL
Jussi Pohjolainen
 
PHP with MySQL
wahidullah mudaser
 
lab56_db
tutorialsruby
 
lab56_db
tutorialsruby
 
Diva10
diva23
 
Ad

More from mussawir20 (20)

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

Recently uploaded (20)

PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 

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 (); ?>