More Related Content Similar to Php My Sql (20) More from mussawir20 (20) Php My Sql3. 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 = "localhost" ; $user = "test" ; $pass = "test" ; $db = "testdb" ; // open connection $connection = mysql_connect ( $host , $user , $pass ) or die ( "Unable to connect!" ); // select database mysql_select_db ( $db ) or die ( "Unable to select database!" ); // create query $query = "SELECT * FROM symbols" ; // execute query $result = mysql_query ( $query ) or die ( "Error in query: $query. " . mysql_error ()); Cont … 5. // see if any rows were returned if ( mysql_num_rows ( $result ) > 0 ) { // yes // print them one after another echo "<table cellpadding=10 border=1>" ; while( $row = mysql_fetch_row ( $result )) { echo "<tr>" ; echo "<td>" . $row [ 0 ]. "</td>" ; echo "<td>" . $row [ 1 ]. "</td>" ; echo "<td>" . $row [ 2 ]. "</td>" ; echo "</tr>" ; } echo "</table>" ; } else { // no // print status message echo "No rows found!" ; } // 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 = "localhost" ; $user = "root" ; $pass = "guessme" ; $db = "testdb" ; $connection = mysql_connect ( $host , $user , $pass ) or die ( "Unable to connect!" ); // get database list $query = "SHOW DATABASES" ; $result = mysql_query ( $query ) or die ( "Error in query: $query. " . mysql_error ()); echo "<ul>" ; while ( $row = mysql_fetch_array ( $result )) { echo "<li>" . $row [ 0 ]; // for each database, get table list and print $query2 = "SHOW TABLES FROM " . $row [ 0 ]; $result2 = mysql_query ( $query2 ) or die ( "Error in query: $query2. " . mysql_error ()); echo "<ul>" ; while ( $row2 = mysql_fetch_array ( $result2 )) { echo "<li>" . $row2 [ 0 ]; } echo "</ul>" ; } echo "</ul>" ; // get version and host information echo "Client version: " . mysql_get_client_info (). "<br />" ; echo "Server version: " . mysql_get_server_info (). "<br />" ; echo "Protocol version: " . mysql_get_proto_info (). "<br />" ; echo "Host: " . mysql_get_host_info (). "<br />" ; // 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 "<table cellpadding=10 border=1>" ; while(list( $id , $country , $animal ) = mysql_fetch_row ( $result )) { echo "<tr>" ; echo "<td>$id</td>" ; echo "<td>$country</td>" ; echo "<td>$animal</td>" ; echo "</tr>" ; } echo "</table>" ; } else { // no // print status message echo "No rows found!" ; } OUTPUT 11. mysql_fetch_assoc() // see if any rows were returned if ( mysql_num_rows ( $result ) > 0 ) { // yes // print them one after another echo "<table cellpadding=10 border=1>" ; while( $row = mysql_fetch_assoc ( $result )) { echo "<tr>" ; echo "<td>" . $row [ 'id' ]. "</td>" ; echo "<td>" . $row [ 'country' ]. "</td>" ; echo "<td>" . $row [ 'animal' ]. "</td>" ; echo "</tr>" ; } echo "</table>" ; } else { // no // print status message echo "No rows found!" ; } OUTPUT 12. mysql_fetch_object() // see if any rows were returned if ( mysql_num_rows ( $result ) > 0 ) { // yes // print them one after another echo "<table cellpadding=10 border=1>" ; while( $row = mysql_fetch_object ( $result )) { echo "<tr>" ; echo "<td>" . $row -> id . "</td>" ; echo "<td>" . $row -> country . "</td>" ; echo "<td>" . $row -> animal . "</td>" ; echo "</tr>" ; } echo "</table>" ; } else { // no // print status message echo "No rows found!" ; } OUTPUT 13. Form application in php and mysql database <html> <head> <basefont face="Arial"> </head> <body> <?php if (!isset( $_POST [ 'submit' ])) { // form not submitted ?> <form action=" <?=$_SERVER [ 'PHP_SELF' ] ?> " method="post"> Country: <input type="text" name="country"> National animal: <input type="text" name="animal"> <input type="submit" name="submit"> </form> Cont … 14. <?php } else { // form submitted // set server access variables $host = "localhost" ; $user = "test" ; $pass = "test" ; $db = "testdb" ; // get form input // check to make sure it's all there // escape input values for greater safety $country = empty( $_POST [ 'country' ]) ? die ( "ERROR: Enter a country" ) : mysql_escape_string ( $_POST [ 'country' ]); $animal = empty( $_POST [ 'animal' ]) ? die ( "ERROR: Enter an animal" ) : mysql_escape_string ( $_POST [ 'animal' ]); // open connection $connection = mysql_connect ( $host , $user , $pass ) or die ( "Unable to connect!" ); Cont … 15. // select database mysql_select_db ( $db ) or die ( "Unable to select database!" ); // create query $query = "INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')" ; // execute query $result = mysql_query ( $query ) or die ( "Error in query: $query. " . mysql_error ()); // print message with ID of inserted record echo "New record inserted with ID " . mysql_insert_id (); // close connection mysql_close ( $connection ); } ?> </body> </html> 17. mysqli library <html> <head> <basefont face="Arial"> </head> <body> <?php // set server access variables $host = "localhost" ; $user = "test" ; $pass = "test" ; $db = "testdb" ; // create mysqli object // open connection $mysqli = new mysqli ( $host , $user , $pass , $db ); // check for connection errors if ( mysqli_connect_errno ()) { die( "Unable to connect!" ); } // create query $query = "SELECT * FROM symbols" ; 18. if ( $result = $mysqli -> query ( $query )) { // see if any rows were returned if ( $result -> num_rows > 0 ) { // yes // print them one after another echo "<table cellpadding=10 border=1>" ; while( $row = $result -> fetch_array ()) { echo "<tr>" ; echo "<td>" . $row [ 0 ]. "</td>" ; echo "<td>" . $row [ 1 ]. "</td>" ; echo "<td>" . $row [ 2 ]. "</td>" ; echo "</tr>" ; } echo "</table>" ; } else { // no // print status message echo "No rows found!" ; } // free result set memory $result -> close (); } else { // print error message echo "Error in query: $query. " . $mysqli -> error ; } // close connection $mysqli -> close (); ?> </body> </html> 20. Delete record from mysqli library <?php // set server access variables $host = "localhost" ; $user = "test" ; $pass = "test" ; $db = "testdb" ; // create mysqli object // open connection $mysqli = new mysqli ( $host , $user , $pass , $db ); // check for connection errors if ( mysqli_connect_errno ()) { die( "Unable to connect!" ); } // if id provided, then delete that record if (isset( $_GET [ 'id' ])) { // create query to delete record $query = "DELETE FROM symbols WHERE id = " . $_GET [ 'id' ]; Cont … 21. // execute query if ( $mysqli -> query ( $query )) { // print number of affected rows echo $mysqli -> affected_rows . " row(s) affected" ; } else { // print error message echo "Error in query: $query. " . $mysqli -> error ; } } // query to get records $query = "SELECT * FROM symbols" ; 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 "<table cellpadding=10 border=1>" ; while( $row = $result -> fetch_array ()) { echo "<tr>" ; echo "<td>" . $row [ 0 ]. "</td>" ; echo "<td>" . $row [ 1 ]. "</td>" ; echo "<td>" . $row [ 2 ]. "</td>" ; echo "<td><a href=" . $_SERVER [ 'PHP_SELF' ]. "?id=" . $row [ 0 ]. ">Delete</a></td>" ; echo "</tr>" ; } } // free result set memory $result -> close (); } else { // print error message echo "Error in query: $query. " . $mysqli -> error ; } // close connection $mysqli -> close (); ?>