The document discusses connecting to and interacting with MySQL databases from PHP. It provides examples of creating a database and table, inserting data, and retrieving data using the mysql and mysqli extensions. Key points covered include connecting to the database, executing queries, and fetching rows of data using functions like mysql_query(), mysql_fetch_array(), and mysqli->query().
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 = "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 ); ?>