Download presentation
Presentation is loading. Please wait.
1
PHP and MySQL
2
Why Use a Database Easy access to data Simultaneous access by multiple users is handled properly Security - easy to control access to data
3
Databases A database is a collection of data organized for efficient access A relational database is a collection of tables Columns represent attributes Rows represent entities We will use MySQL for our examples
4
MySQL MySQL is a free database system which is running on onyx Standard port for MySQL is 3306 For this class, use the user name php_user and the password web2dtbs php_user has access to the restaurant database and all databases whose names begin with test php_user has enough privileges to create and use any database whose name has the form test_* Please, do not drop any databases from the system except your own.
5
Interacting with MySQL from the shell Starting the command-line client mysql -p -u php_user Find out what databases exist show databases \g Select a database to use use databaseName \g Show all the tables in the database show tables \g Quit \q
6
Creating a new database You could do this from a PHP program Probably easier to do it manually - you don't usually want to do it more than once Create a set of files Define the tables Load initial data into the tables The directory ~tcole/teaching/cs498/demo/db on onyx has the files used to set up the restaurant database
7
Using MySQL from a PHP program PHP has a collection of add-on modules called PEAR (PHP Extension and Application Repository) One of the modules is DB which has a set of functions for interacting with databases You write the same PHP code for any database program Alternately, there are separate extensions for each database program (mysql for MySQL)
8
Using PEAR to connect Load the DB module require 'DB.php'; Connecting Use the connect function with a Data Source Name argument specifying host, user, database client, database Get back an object you can use to interact with the database $db = DB::connect( 'mysql://user:password@hostname/database');
9
Checking for Success The isError method in the DB module will tell yow whether you connected successfully DB::isError($db) returns true if there was a problem The database object has a getMessage method which provides some information $db->getMessage() We can set the error handling mode $db->setErrorHandling( PEAR_ERROR_DIE);
10
Interacting with the database The database object has a query method that takes an SQL command as its argument $q = $db->query( queryString) the method returns an object the returned object can be tested by DB::isError( $q)
11
Retrieving Data Use the SQL SELECT command in the query string to retrieve data from the database $q = $db->query( 'SELECT dish_name, price FROM dishes'); The returned object gives you access to the returned data $row = $q->fetchRow() returns either the next row (as an array) or false if there are no more $q->numRows() returns the number of rows $q->getAll() returns an array containing all the rows
12
Inserting data Use the SQL INSERT command in your query string $q = $db->query( "INSERT INTO dishes (dish_name, price, is_spicy) VALUES ('Sesame Seed Puff', 2.50, 0)"); There are several forms for INSERT
13
Inserting Data from Forms You can use form data either to create new rows or as part of the WHERE clause in a SELECT command To make your database safe from malicious users, use placeholders to put form data into a query string $db->query( 'INSERT INTO dishes (dish_name, price) VALUES (?, ?)', array( $_POST['new_dish_name'], $_POST['new_price'])); This form of query takes care of single quotes in the entered text
14
Modifying data Use the SQL UPDATE command in your query string $q = $db->query( "UPDATE dishes SET price = price * 1.1"); There are several forms for UPDATE generally want to use a where clause to limit which rows are updated
15
Removing Data Use the SQL DELETE command in your query string $q = $db->query( "DELETE FROM dishes WHERE price < 1.00"); Without the WHERE, all rows are deleted You can check how many rows were removed with $db->affectedRows()
16
MySQL without PEAR You can use databases without using PEAR Commands are different for each database Use mysql (or mysqli) for MySQL databases Which you use depends on what versions are being used; mysql seems to work on onyx
17
mysql Commands Commands $db = mysql_connect( host, user, pwd, db) $q = mysql_query($db,sql) $row = mysql_fetch_row($q) msql_affected_rows( $q) mysql_num_rows( $q) There is no automatic error handling but you can use mysql_connect_error() to check manually
18
Sources Learning PHP 5 by David Sklar Web Database Applications with PHP and MySQL by Hugh E. Williams and David Lane PHP home page http://www.php.net/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.