210 likes | 315 Views
Session 7: Getting PHP to Talk to MySQL. Objectives. Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data Using PEAR. The Process. Connect to the database Select the database to use Build a SELECT statement Perform the query
E N D
Objectives • Connecting to the Database • Building & Executing the SQL SELECT Query • Fetching & Displaying the data • Using PEAR
The Process • Connect to the database • Select the database to use • Build a SELECT statement • Perform the query • Display the result
Selecting the Database • All mysql related functions have an optional parameter – the database connection. If you don’t specify the connection, the connection is the latest result of calling mysql_connect
Building the SQL SELECT Query • Use a variable named $query to store the SQL Statement • Build up a query in parts using the string concatenation (.) operator
Executing the Query • Using the mysql_query() function • The results form a result set. Each element corresponds to a row. • To display them, process each row, one at a time
Fetching and Displaying • Use mysql_fetch_row() function to get the rows from the result set • The function returns one row at a time until there are no more rows. • If there are no more rows, it return FALSE. • Thus, use a loop to fetch and display the result set.
Fetching and Displaying (cont.) • Use mysql_fetch_array() function • First parameter: the result set • Second parameter (optional) • MYSQL_ASSOC, array is indexed based on columns’s names • MYSQL_NUM, array is index based on number starting at zero • MYSQL_BOTH, both types above
Fetching and Displaying (cont.) • Use mysql_fetch_assoc() function
Closing Connection • Close the connection when finishing • Closing the connection will free any resources and memory allocated to it by MySQL and PHP • Syntax: mysql_close($connection)
Using PEAR • PEAR is a framework and distribution system for reusable PHP components • There are many modules which is categorized
Installing PEAR • Go to the command line window • Go to the path C:\php • Type go-pear.bat • At the first question, type local • Confirm by typing yes
Installing PEAR (cont.) • At the next question, type all
Installing PEAR (cont.) • Answer 12 question as following
Installing PEAR (cont.) • Then Enter • At the altering php.ini question, type Y • Then Enter • And finish installing of PEAR
Adding Additional Package • Access PEAR Package Manager by entering pear at the command prompt • Adding new modules: pear install package_name
Using PEAR DB • Include the PEAR DB package • Create a connection • Connect string • Dbtype: dbase, msql, mssql, mysql, mysqli, oci8, odbc, pgsql, … • Querying • Fetching
Using PEAR DB (cont.) • Closing • PEAR error reporting
Practice • Develop an Image Collection System with following functions • User Module • Register a new user • Change password • Edit user profile • Logon/logout • Album Module • Create a new, empty album • Upload images to the album • Delete images of an album • Sort images based on size, name, date of upload • View album • Task: • Design the database structure • Create the database with MySQL