PHP and MySQL Web Development tMyn1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
PHP (2) – Functions, Arrays, Databases, and sessions.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Objectives Connect to MySQL from PHP
Website Development & Management PHP Odds & Ends Instructor: John Seydel, Ph.D. CIT Fall
Intermediate PHP & MySQL
Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
What is MySQL? MySQL is a database. The data in MySQL is stored in database objects called tables. A table is a collections of related data entries and.
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
INTERNET APPLICATION DEVELOPMENT For More visit:
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
Server-side Scripting Powering the webs favourite services.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP – MySQL Extensions. Table used in most examples CREATE TABLE product ( rowID INT NOT NULL AUTO_INCREMENT, productid VARCHAR(8) NOT NULL, name VARCHAR(25)
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
PHP meets MySQL.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Creating PHPs to Insert, Update, and Delete Data CS 320.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database Processing CIS 1715 Web Technologies.
PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in PHP consists of a number of functions.
Web Database Programming Using PHP
SQL and SQL*Plus Interaction
Web Database Programming Using PHP
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
ISC440: Web Programming 2 Server-side Scripting PHP 3
Erasmus Exchange in Ionian University
MySQL Web Application Connecting to a MySQL database
Presentation transcript:

PHP and MySQL Web Development tMyn1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in PHP consists of a number of functions you can call to interact with MySQL, and here are some of them:

PHP and MySQL Web Development tMyn2 The mysql_connect() function opens a non-persistent MySQL connection. This function returns the connection on success, or FALSE and an error on failure. Syntax mysql_connect(server,user,pwd,newlink,clientflag) ParameterDescription serverOptional. Specifies the server to connect to (can also include a port number, e.g. "hostname:port" or a path to a local socket for the localhost). Default value is "localhost:3306" userOptional. Specifies the username to log in with. Default value is the name of the user that owns the server process pwdOptional. Specifies the password to log in with. Default is ""

PHP and MySQL Web Development tMyn3 The mysql_select_db() function sets the active MySQL database. This function returns TRUE on success, or FALSE on failure. Syntax mysql_select_db(database,connection) ParameterDescription databaseRequired. Specifies the database to select. connectionOptional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

PHP and MySQL Web Development tMyn4 The mysql_query() function executes a query on a MySQL database. This function returns the query handle for SELECT queries, TRUE/FALSE for other queries, or FALSE on failure. Syntax mysql_query(query,connection) ParameterDescription queryRequired. Specifies the SQL query to send (should not end with a semicolon). connectionOptional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

PHP and MySQL Web Development tMyn5 The mysql_fetch_array() function returns a row from a recordset as an associative array and/or a numeric array. This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows. Syntax mysql_fetch_array(data,array_type) ParameterDescription dataRequired. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function array_typeOptional. Specifies what kind of array to return. Possible values: MYSQL_ASSOC - Associative array MYSQL_NUM - Numeric array MYSQL_BOTH - Default. Both associative and numeric array

PHP and MySQL Web Development tMyn6 The mysql_fetch_object() function returns a row from a recordset as an object. This function gets a row from the mysql_query() function and returns an object on success, or FALSE on failure or when there are no more rows. Syntax mysql_fetch_object(data) ParameterDescription dataRequired. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function Tips and Notes Note: Each subsequent call to mysql_fetch_object() returns the next row in the recordset.

PHP and MySQL Web Development tMyn7 The mysql_affected_rows() function returns the number of affected rows in the previous MySQL operation. This function returns the number of affected rows on success, or -1 if the last operation failed. Syntax mysql_affected_rows(connection) ParameterDescription connectionOptional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

PHP and MySQL Web Development tMyn8 The mysql_num_rows() function returns the number of rows in a recordset. This function returns FALSE on failure. Syntax mysql_num_rows(data) ParameterDescription dataRequired. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function

PHP and MySQL Web Development tMyn9 The mysql_result() function returns the value of a field in a recordset. This function returns the field value on success, or FALSE on failure. Syntax mysql_result(data,row,field) ParameterDescription dataRequired. Specifies which result handle to use. The data pointer is the return from the mysql_query() function rowRequired. Specifies which row number to get. Row numbers start at 0

PHP and MySQL Web Development tMyn10 fieldOptional. Specifies which field to get. Can be field offset, field name or table.fieldname. If this parameter is not defined mysql_result() gets the first field from the specified row. Tips and Notes This function is slower than mysql_fetch_row(), mysql_fetch_array(), mysql_fetch_assoc() and mysql_fetch_object().

PHP and MySQL Web Development tMyn11 The mysql_error() function returns the error description of the last MySQL operation. This function returns an empty string ("") if no error occurs. Syntax mysql_error(connection) ParameterDescription connectionOptional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

PHP and MySQL Web Development tMyn12 The mysql_close() function closes a non-persistent MySQL connection. This function returns TRUE on success, or FALSE on failure. Syntax mysql_close(connection) ParameterDescription connectionOptional. Specifies the MySQL connection to close. If not specified, the last connection opened by mysql_connect() is used.

PHP and MySQL Web Development tMyn13 die — Equivalent to exit() Description This language construct is equivalent to exit().

PHP and MySQL Web Development tMyn14 exit — Output a message and terminate the current script Description void exit ([ string $status ] ) void exit ( int $status ) Terminates execution of the script. Parameters status If status is a string, this function prints the status just before exiting. If status is an integer, that value will also be used as the exit status. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

PHP and MySQL Web Development tMyn15 A typical web database transaction consists of the following stages, which are numbered in the Figure 1: 1. A user’s web browser issues an HTTP request for a particular web page. For example, using an HTML form, she might have requested a search for all books at MikkeliOnlineProfessionalBooks.com written by Leila Karjalainen. The search results page is called results.php. 2. The web server receives the request for results.php, retrieves the file, and passes it to the PHP engine for processing.

PHP and MySQL Web Development tMyn MySQL Server BrowserWeb Server PHP Engine

PHP and MySQL Web Development tMyn17 3. The PHP engine begins parsing the script. Inside the script is a command to connect to the database and execute a query (perform the search for books). PHP opens a connection to the MySQL server and sends on the appropriate query. 4. The MySQL server receives the database query, processes it, and sends the results - a list of books - back to the PHP engine. 5. The PHP engine finishes running the script, which usually involves formatting the query results nicely in HTML. It then returns the resulting HTML to the web server.

PHP and MySQL Web Development tMyn18 6. The web server passes the HTML back to the browser, where the user can see the list of books she requested. The above described process is basically the same regardless of which scripting engine or database server you use. Sometimes the web server, PHP engine, and database server all run on the same machine. However, it is quite common for the database server to run on a different machine. You might do this for reasons of security, increased capacity, or load spreading. From a development perspective, this approach is much the same to work with.

PHP and MySQL Web Development tMyn19 First example reads in and displays the contents of the Friend table from the database Future. Our script will do the following jobs: –Set up a connection to the appropriate database –Query the database table –Retrieve the results –Present the results back to the user First we need to create the needed database and database table – this time we will do it directly using MySQL Query Browser:

PHP and MySQL Web Development tMyn20

PHP and MySQL Web Development tMyn21

PHP and MySQL Web Development tMyn22

PHP and MySQL Web Development tMyn23 Next the PHP script:

PHP and MySQL Web Development tMyn24

PHP and MySQL Web Development tMyn25

PHP and MySQL Web Development tMyn26 …and what you can see from the browser:

PHP and MySQL Web Development tMyn27 ‘$sqlResult = mysql_query...’ When you select items from a database using mysql_query(), the data is returned as a MySQL result. Since we want to use this data in our program we need to store it in a variable. $sqlResult now holds the result from our mysql_query().

PHP and MySQL Web Development tMyn28 ‘while($sqlRow = mysql_fetch_array( $sqlResult…)’ The mysql_fetch_array function gets the next-in-line associative array from a MySQL result. By putting it in a while loop it will continue to fetch the next array until there is no next array to fetch. This function can be called as many times as you want, but it will return FALSE when the last associative array has already been returned. By placing this function within the conditional statement of the while loop, we can “kill” two birds with one stone:

PHP and MySQL Web Development tMyn29 1. We can retrieve the next associative array from our MySQL resource, $sqlResult, so that we can print out the retrieved information. 2. We can tell the while loop to stop printing out information when the MySQL resource has returned the last array, as FALSE is returned when it reaches the end and this will cause the while loop to halt. A resource is a special variable, holding a reference to an external resource.

PHP and MySQL Web Development tMyn30 In the above script, we have accessed the firstName column like this: $sqlRow[‘firstName’]. That can also be done by using integer indexing:

PHP and MySQL Web Development tMyn31

PHP and MySQL Web Development tMyn32 Or finding out the number of rows in a recordset:

PHP and MySQL Web Development tMyn33

PHP and MySQL Web Development tMyn34

PHP and MySQL Web Development tMyn35 Or returning a row from a recordset as an object

PHP and MySQL Web Development tMyn36

PHP and MySQL Web Development tMyn37

PHP and MySQL Web Development tMyn38 A minor modification to the original example: let’s make it display a message if there is an error when connecting to the database server:

PHP and MySQL Web Development tMyn39

PHP and MySQL Web Development tMyn40

PHP and MySQL Web Development tMyn41 So it seems that die() needs no arguments because mysql_connect() is able to give the same information:

PHP and MySQL Web Development tMyn42

PHP and MySQL Web Development tMyn43

PHP and MySQL Web Development tMyn44 A minor modification to the original example: let’s make it display a message if there is an error when selecting the database we want to use:

PHP and MySQL Web Development tMyn45

PHP and MySQL Web Development tMyn46

PHP and MySQL Web Development tMyn47 In the next example we will insert one row to the Friend table. First directly from web server to the database server without any user interface.

PHP and MySQL Web Development tMyn48

PHP and MySQL Web Development tMyn49

PHP and MySQL Web Development tMyn50

PHP and MySQL Web Development tMyn51 Abit more complex task: Insert data from a form into a database: Now we will create an HTML form that can be used to add new records to the Friend table, file database3.html:

PHP and MySQL Web Development tMyn52

PHP and MySQL Web Development tMyn53 When a user clicks the submit button in the HTML form in the example above, the form data is sent to database3.php. The database3.php file connects to a database, and retrieves the values from the form with the PHP $_POST variables. Then, the mysql_query() function executes the INSERT INTO statement, and a new record will be added to the Friend table. Here is the database3.php page:

PHP and MySQL Web Development tMyn54

PHP and MySQL Web Development tMyn55

PHP and MySQL Web Development tMyn56

PHP and MySQL Web Development tMyn57

PHP and MySQL Web Development tMyn58 From the previous slide it can be seen that the primary key jumps from 4 to 9 - that is because there were some errors when testing the example… The primary key can be modified directly using MySQL Query Browser (Naturally UPDATES can be done using our HTML user interface) if the current situation annoys someone:

PHP and MySQL Web Development tMyn59

PHP and MySQL Web Development tMyn60 A minor modification to the database3.php example: Let’s test that all the HTML fields have at least something inputted:

PHP and MySQL Web Development tMyn61

PHP and MySQL Web Development tMyn62

PHP and MySQL Web Development tMyn63

PHP and MySQL Web Development tMyn64

PHP and MySQL Web Development tMyn65

PHP and MySQL Web Development tMyn66

PHP and MySQL Web Development tMyn67 A minor modification to the database3.php example: putting it all in one page. The first test: has the user submitted the form? Second test: Is there something in every field?

PHP and MySQL Web Development tMyn68

PHP and MySQL Web Development tMyn69

PHP and MySQL Web Development tMyn70

PHP and MySQL Web Development tMyn71

PHP and MySQL Web Development tMyn72

PHP and MySQL Web Development tMyn73

PHP and MySQL Web Development tMyn74 If every field does have something:

PHP and MySQL Web Development tMyn75

PHP and MySQL Web Development tMyn76

PHP and MySQL Web Development tMyn77 If there is one or more empty fields:

PHP and MySQL Web Development tMyn78

PHP and MySQL Web Development tMyn79

PHP and MySQL Web Development tMyn80

PHP and MySQL Web Development tMyn81

PHP and MySQL Web Development tMyn82 Update data from a form into a database: Now we will create an HTML form that can be used to update one column in the Friend table, file update1.php. We have arbitrarily chosen to update the first name of the person. The first test: has the user submitted the form? Second test: is there something in every field? Third test: is the new first name longer than the field domain permits?

PHP and MySQL Web Development tMyn83

PHP and MySQL Web Development tMyn84

PHP and MySQL Web Development tMyn85

PHP and MySQL Web Development tMyn86

PHP and MySQL Web Development tMyn87

PHP and MySQL Web Development tMyn88

PHP and MySQL Web Development tMyn89 Let’s find out what there is in the table Friend:

PHP and MySQL Web Development tMyn90

PHP and MySQL Web Development tMyn91 The task is to update the first name Rosalind to Rosalind Elsie :

PHP and MySQL Web Development tMyn92

PHP and MySQL Web Development tMyn93

PHP and MySQL Web Development tMyn94 The third test: has the user inputted too long a name?:

PHP and MySQL Web Development tMyn95 New First Name: Longer than 45 character constants

PHP and MySQL Web Development tMyn96

PHP and MySQL Web Development tMyn97 One more detail from the previous example: the first parameter of the mysql_query() is UPDATE statement. UPDATE and DELETE statements behave in the same way in those kinds of situations: if there are no rows to be updated or deleted, then there would not come any warnings or errors back:

PHP and MySQL Web Development tMyn98

PHP and MySQL Web Development tMyn99

PHP and MySQL Web Development tMyn100 Definitely we need to improve the source code:

PHP and MySQL Web Development tMyn101

PHP and MySQL Web Development tMyn102

PHP and MySQL Web Development tMyn103 Let us test the modification. The second name and the address are valid values:

PHP and MySQL Web Development tMyn104

PHP and MySQL Web Development tMyn105

PHP and MySQL Web Development tMyn106 The following example selects the same data as the example above, but will display the data in an HTML table:

PHP and MySQL Web Development tMyn107

PHP and MySQL Web Development tMyn108

PHP and MySQL Web Development tMyn109