Download presentation
Presentation is loading. Please wait.
Published byEmma Chapman Modified over 9 years ago
1
PHP Database Processing CIS 1715 Web Technologies
2
PHP Database Processing In this exercise, we will learn how to create a dynamic XTML Select list from data stored in our states table.
3
Select List Markup Alabama Alaska Arkansas. Washington We can create the select list dynamically with PHP by querying the states table Each option element is echoed out in a while loop
4
PHP Database Processing In this unit we will cover the PHP elements required to process MySQL databases – Database Concepts – Connecting to the database mysql_connect() – Selecting the database to use mysql_select_db() – Perform required database processing Create Read Update Delete – Closing the database connection mysql_close()
5
States Table Fields
6
PHP Database Processing Example $db_connection = mysql_connect (“localhost”, “webdev”, “webdevpass”) or die ( mysql_error() ) ; – This statement establishes a connection to the database using the constant values – If the connection fails, we stop processing with the die() function
7
PHP Database Processing Example $db_select = mysql_select_db(“cis1715”) or die ( mysql_error() ); – This statement selects which database will be use in subsequent SQL queries – If the function fails we stop processing using the die() function
8
PHP Database Processing Once the connection has been established and the database has been selected, we can begin executing SQL statements The SQL SELECT statement is used to read data from the states table in the database
9
mysql_query() Function Syntax – mysql_query(‘sql_statement’) – Where sql_statement is an valid SQL statement Example $query_result = mysql_query (“SELECT * FROM states“) or die (mysql_error()); $query_result is a reference to the returned record set
10
PHP Database Processing Once the query is executed, we need to loop through the returned record set This is done using a while loop in combination with the mysql_fetch_assoc() function The mysql_fetch_assoc() function accepts one argument and returns the fields of the fetched rows as an associative array
11
PHP Database Processing Syntax – mysql_fetch_assoc(recordset_ref) Where recordset_ref is the reference to the returned recordset (from previous SQL statement)
12
PHP Database Processing Example $row = mysql_fetch_assoc($query_result) – In this example fields are fetched from the query result and placed in the $row table – The individual fields in the fetched row are referenced using the following syntax $row[‘fieldname’]
13
PHP Database Processing The mysql_fetch_assoc() function is used with a while loop to loop through all the rows of the returned recordset The mysql_fetch_assoc() returns true as long as there are rows in the recordset
14
PHP Database Processing Example while ($row = mysql_fetch_assoc($query_result)) { echo $row[‘lname’]; } In this example, we loop through the returned recordset and display the lname field of each row The lname field is a field in the table queried
15
PHP Database Processing After we have looped through the returned recordset we release resources and close the connection as follows – mysql_free_result($query_result); – mysql_close();
16
PHP Database Processing The following example illustrates PHP database processing used in the context of an HTML document for format a table
17
<?php // connect to database $db_connection = mysql_connect (“localhost”, “webdev”, “webdevpass”) or die ( mysql_error() ) ; // select database to use $db_select = mysql_select_db(“cis1715”) or die ( mysql_error() ); // Execute query. Select all customer rows $query_result = mysql_query (“SELECT * FROM states”) or die ( mysql_error() ); ?>
18
<?php while ($row = mysql_fetch_assoc($query_result)) { ?> ”> <?php } // end while loop mysql_free_result($query_result); mysql_close(); ?>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.