session 7: getting php to talk to mysql
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/1.jpg)
Session 7: Getting PHP to Talk to MySQL
![Page 2: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/2.jpg)
Objectives
Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data Using PEAR
![Page 3: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/3.jpg)
The Process
Connect to the database Select the database to use Build a SELECT statement Perform the query Display the result
![Page 4: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/4.jpg)
Including Database Login Details
![Page 5: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/5.jpg)
Connecting to the Database
![Page 6: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/6.jpg)
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
![Page 7: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/7.jpg)
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
![Page 8: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/8.jpg)
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
![Page 9: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/9.jpg)
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.
![Page 10: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/10.jpg)
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
![Page 11: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/11.jpg)
Fetching and Displaying (cont.)
Use mysql_fetch_assoc() function
![Page 12: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/12.jpg)
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)
![Page 13: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/13.jpg)
Using PEAR
PEAR is a framework and distribution system for reusable PHP components
There are many modules which is categorized
![Page 14: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/14.jpg)
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
![Page 15: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/15.jpg)
Installing PEAR (cont.)
At the next question, type all
![Page 16: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/16.jpg)
Installing PEAR (cont.)
Answer 12 question as following
![Page 17: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/17.jpg)
Installing PEAR (cont.)
Then Enter At the altering php.ini question, type Y Then Enter And finish installing of PEAR
![Page 18: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/18.jpg)
Adding Additional Package
Access PEAR Package Manager by entering pear at the command prompt
Adding new modules: pear install package_name
![Page 19: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/19.jpg)
Using PEAR DB
Include the PEAR DB package
Create a connection
Connect string
Dbtype: dbase, msql, mssql, mysql, mysqli, oci8, odbc, pgsql, … Querying
Fetching
![Page 20: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/20.jpg)
Using PEAR DB (cont.)
Closing PEAR error reporting
![Page 21: Session 7: Getting PHP to Talk to MySQL](https://reader035.vdocument.in/reader035/viewer/2022082207/56813757550346895d9ee833/html5/thumbnails/21.jpg)
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