Connecting To A Database Using PHP: Prof. Jim Whitehead CMPS 183, Spring 2006 May 15, 2006
Connecting To A Database Using PHP: Prof. Jim Whitehead CMPS 183, Spring 2006 May 15, 2006
Using PHP
Prof. Jim Whitehead
CMPS 183, Spring 2006
May 15, 2006
Rationale
Most Web applications:
Retrieve information from a database to alter
their on-screen display
Store user data such as orders, tracking,
address, credit card, etc. in a database
Parameters
Server hostname of server
Username username on the database
Password password on the database
New Link (mysql_connect only) reuse database connection
created by previous call to mysql_connect
Client Flags
Selecting a Database
mysql_select_db()
Pass it the database name
Related:
mysql_list_dbs()
List databases available
Mysql_list_tables()
List database tables available
If there is an error
mysql_error() returns error string from last MySQL call
Process Results
Many functions exist to work with database
results
mysql_num_rows()
Number of rows in the result set
Useful for iterating over result set
mysql_fetch_array()
Returns a result row as an array
Can be associative or numeric or both (default)
$row = mysql_fetch_array($result);
$row[column name] :: value comes from database
row with specified column name
$row[0] :: value comes from first field in result set