Download presentation
Presentation is loading. Please wait.
Published byElvin Neal Modified over 9 years ago
1
Session 7: Getting PHP to Talk to MySQL
2
Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data Using PEAR
3
The Process Connect to the database Select the database to use Build a SELECT statement Perform the query Display the result
4
Including Database Login Details
5
Connecting to the Database
6
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
7
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
8
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
9
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.
10
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
11
Fetching and Displaying (cont.) Use mysql_fetch_assoc() function
12
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)
13
Using PEAR PEAR is a framework and distribution system for reusable PHP components There are many modules which is categorized
14
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
15
Installing PEAR (cont.) At the next question, type all
16
Installing PEAR (cont.) Answer 12 question as following
17
Installing PEAR (cont.) Then Enter At the altering php.ini question, type Y Then Enter And finish installing of PEAR
18
Adding Additional Package Access PEAR Package Manager by entering pear at the command prompt Adding new modules: pear install package_name
19
Using PEAR DB Include the PEAR DB package Create a connection Connect string Dbtype: dbase, msql, mssql, mysql, mysqli, oci8, odbc, pgsql, … Querying Fetching
20
Using PEAR DB (cont.) Closing PEAR error reporting
21
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.