PHP and MySQL - Jacksonville University

Download Report

Transcript PHP and MySQL - Jacksonville University

1
PHP and MySQL
CS380
How Web Site Architectures Work
2







User’s browser sends HTTP request.
The request may be a form where the action is to
call PHP code (ex. results .php).
Web server receives the request.
Web server passes the file (results.php) to the PHP
engine.
PHP engine parses the script.
PHP opens connection to MySQL server if needed.
PHP sends query to database server.
CS380
How Web Site Architectures Work
3






Database server processes the query.
Database sends back results to PHP server.
PHP formats data that it received from database
server nicely for HTML.
PHP engine finishes running script.
PHP returns HTML to the web server.
Web server passes HTML back to the browser.
CS380
Querying a database from the web
4





Check and filter data coming from the user.
Setup connection to the appropriate database.
Query the database.
Retrieve the results.
Present the results back to user.
CS380
PHP MySQL functions
5
name
description
mysql_connect
connects to a database server
mysql_select_db
chooses which database on server to
use (similar to SQL USE database;
command)
mysql_query
performs a SQL query on the
database
mysql_real_escape_string
encodes a value to make it safe for
use in a query
mysql_fetch_array, ...
returns the query's next result row as
an associative array
mysql_close
closes a connection to a database
CS380
Other MySQL PHP functions
6
name
description
mysql_num_rows
returns number of rows matched by
the query
mysql_num_fields
returns number of columns per result in
the query
mysql_list_dbs
returns a list of databases on this
server
mysql_list_tables
returns a list of tables in current
database
mysql_list_fields
returns a list of fields in the current
data
complete list
CS380
Insert in a database
7
INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
SQL

Insert new values to columns of a table
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];
$query = "insert into books values
('".$isbn."', '".$author."', '".$title."', '".$price."')";
SQL
CS380
Practice: Bookorama
8


Query database using forms
Insert data in database
CS380