session 7: getting php to talk to mysql

21
Session 7: Getting PHP to Talk to MySQL

Upload: sierra-patel

Post on 04-Jan-2016

67 views

Category:

Documents


6 download

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 Presentation

TRANSCRIPT

Page 1: Session 7: Getting PHP to Talk to MySQL

Session 7: Getting PHP to Talk to MySQL

Page 2: 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

Page 3: Session 7: Getting PHP to Talk to MySQL

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

Including Database Login Details

Page 5: Session 7: Getting PHP to Talk to MySQL

Connecting to the Database

Page 6: Session 7: Getting PHP to Talk to MySQL

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

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

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

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

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

Fetching and Displaying (cont.)

Use mysql_fetch_assoc() function

Page 12: Session 7: Getting PHP to Talk to MySQL

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

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

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

Installing PEAR (cont.)

At the next question, type all

Page 16: Session 7: Getting PHP to Talk to MySQL

Installing PEAR (cont.)

Answer 12 question as following

Page 17: Session 7: Getting PHP to Talk to MySQL

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

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

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

Using PEAR DB (cont.)

Closing PEAR error reporting

Page 21: Session 7: Getting PHP to Talk to MySQL

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