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

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