Docsity

PHP-MySQL Database Applications - Lecture Slides | CSC 6370, Study notes of Computer Science

Partial preview of the text

Download PHP-MySQL Database Applications - Lecture Slides | CSC 6370 and more Study notes Computer Science in PDF only on Docsity!

PHP-MySQL databaseapplications

PHP-MySQL database applications „ Brief review of MySQL „ PHP MySQL functions „ examples

Command Line Client „ Read the MySQL documentation „ c:\mysql\mysql\docs\manual_toc.html „ Read MySQL install instructions on CD „ Command to enter monitor is „ mysql - u xxxx -p „ To execute an SQL script use „ mysql -u xxxx -p < /path/script.sql „ GUI client MyCC is better

Supply ausernameand apasswordwhenprompted

Creating Database Tables „ Create^ web_db^ database to hold the tables: „ CREATE DATABASE web_db; „ To create a table called notes: „ USE web_db; „ CREATE TABLE notes (...); „ insert some rows for testing if necessary „ It is easy to write an sql script called notes.sql^ and use monitor to execute it

PHP MySQL Functions „ Connecting to a Database „ Making a query „ Using results of a query „ freeing resources „ closing the connection

„^ mysql_connect(server, username, password)^ „^ connects to a MySQL server through a port^ „^ the default is the string

"localhost:3306"

Connecting To A Database „ username is a string for the user name „ password is a string for the password „ returns FALSE on failure „ Example „ $db_link = mysql_connect("localhost:3306","test", "123"); „ there is also the persistent mysql_pconnect

Selecting A Database „ mysql_select_db(name, link) „ select a database given by the string name „ the link is optional and specifies the open linkvalue such as^ $db_link

returned by a connect

statement. „ if not supplied the last opened link is used. „ returns TRUE on success else FALSE „ Example „ mysql_select_db("web_db");

Example From PHP Docs <?php$lnk =^ mysql_connect('localhost',

'mysql_user', 'mysql_password')or^ die^ ('Not^ connected^ :^ '^.^ mysql_error()); //^ make^ foo^ the^ current

db mysql_select_db('foo',

$lnk)^ or^ die^ ('Can't

use^ foo :^ '^.^ mysql_error());?>

Error Reporting (2) „ mysql_no(link) „ Return the error number „ the link is optional „ if not supplied the last opened link is used. „ 0 is returned if there is no error. „ Example „ mysql_no();

Example From PHP Docs <?phpmysql_connect("localhost",

"mysql_user", "mysql_password");mysql_select_db("nonexistentdb");echo^ mysql_errno()

.^ ":^ "^.^ mysql_error().

**"\n"; mysql_select_db("kossu");mysql_query("SELECT ***^ FROM^ nonexistenttable"); echo^ mysql_errno()^

.^ ":^ "^.^ mysql_error() .^ "\n"; ?>

Making A Query (2) „ INSERT and UPDATE queries „ for these queries a resource is not returned „ TRUE is returned on sucess „ FALSE is returned on failure „ Example (describe the books table) $query = "DESCRIBE books";$status = mysql_query($query);

Example From PHP Docs <?php$result^ =^ mysql_query("SELECT

my_col^ FROM^ my_tbl") or^ die("Invalid^ query:

"^.^ mysql_error()); ?>

Number Of Table Columns „ mysql_num_fields(result) „ return the numbers of columns in a table „ result is the resource returned by a call to themysql_list_fields function „ Example $fields^ =^ mysql_list_fields("web_db",

"books"); $num_columns^ =^ mysql_num_fields($fields);

Names Of Table Columns „ mysql_field_name(result, index) „ return the name of the table column whoseposition is given by index (0,1,...) „ result is the resource returned by a call tomysql_list_fields „ Example: the first column name $fields^ =^ mysql_list_fields("web_db",

"books"); $isbn^ =^ mysql_field_name($fields,