CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:

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

PHP (2) – Functions, Arrays, Databases, and sessions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
PHP and MySQL PHP for the Web, page PHP and MySQL MySQL Resource PHP – MySQL Resource
Objectives Connect to MySQL from PHP
MySQL and PHP By Trevor Adams.
Website Development & Management PHP Odds & Ends Instructor: John Seydel, Ph.D. CIT Fall
Intermediate PHP & MySQL
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.
Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side.
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.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
7/8/05MySQL David Lawrence1 David Lawrence, JLab An introduction for the novice.
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:
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
PHP MySQL Introduction
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.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 19: Database Support.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Comp 519: Web Programming Autumn 2014 Advanced SQL and PHP Advanced queries Querying more than one table Searching tables to find information Aliasing.
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.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
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.
Polling System Part 1 Creating PHP & MySQL Files CIS 254.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.
PHP with MySQL 1.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
MySQL Database Connection
Pengantar Teknologi Internet W14: Server Scripting & Database.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
Intro to PHP Carl-Erik Svensson. What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database Processing CIS 1715 Web Technologies.
THE WEBMASTERS: SENG + WAVERING.  On account of construction, we will be having class in room 1248 next week.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
Accessing mySQL relational database. MySQL database.  Today, we will attempt and open a connection to the MySQL server.  We need to specify the database.
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder.
PHP with MYSQL Please use speaker notes for additional information!
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.
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 ©
Error Handling Summary of the next few pages: Error Handling Cursors.
PHP and MySQL.
Web Programming– UFCFB Lecture
PHP AND MYSQL.
Database Access with PHP and MySQL
Presentation transcript:

CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page: CISC3140-Meyer-lec6 1

Content Connecting to a MySQL Database Disconnecting from a MySQL Database Error Checking Executing SQL Statements Useful Functions Sample Code CISC3140-Meyer-lec6 2

Using MySQL and PHP There are five things you want to be able to do from within PHP in relation to a MySQL database 1.Connect to the MySQL database 2.Disconnect from the MySQL database 3.Execute MySQL queries 4.Work with the results from an SQL query 5.Check the status of your MySQL commands Queries can be any kind of MySQL query, including SELECT,UPDATE,INSERT, etc. Following SELECT queries, you can execute MySQL / PHP functions to put the data read from the MySQL database into your PHP variables CISC3140-Meyer-lec6 3

Connecting to a MySQL database Connecting to the database: $conn = mysql_connect( $mysql_host, $mysql_user, $mysql_password ) or die('Could not connect:'.mysql_error() ); print "Connected successfully"; mysql_select_db($mysql_db) or die('Could not select database’); Replace the variables $mysql_host, $mysql_user, $mysql_password and $mysql_db with strings containing the values for your database Notice that there are two functions invoked One that logs into MySQL: mysql_connect() One that selects the database to use: mysql_select_db() CISC3140-Meyer-lec6 4

On " or die() " According to the PHP manual die() is a "language construct equivalent to exit()."exit() die() can be called independently and used to dump error information to the screen or to a file. "or die()" uses short circuit testing to call die IF the statement preceding die returns false. Increasingly using "or die()" in production code is seen as bad form and/or a security risk… thususing "or die()" in production code is seen as bad form and/or a security risk CISC3140-Meyer-lec6 5

PHP Exceptions PHP also support error handling in ways other than die() including exceptions: if ( !$result = mysql_query('SELECT foo FROM bar', $db) ) { throw new Exception('You fail: '. mysql_error($db)); } Placed within a try/catch block the code above could recover "gracefully" from a db error. Feel free to use "or die()" in this class. See also: trigger_error() CISC3140-Meyer-lec6 6

Disconnecting From the MySQL Database Disconnect from MySQL database: mysql_close( $conn ); CISC3140-Meyer-lec6 7

Check the Status of your MySQL Commands If errors occur, the functions return errors These errors can be read as strings using: mysql_error() Note the usage in this statement: $conn = mysql_connect( $mysql_host, $mysql_user, $mysql_password) or die('Could not connect:’.mysql_error()); echo 'Connected successfully‘; CISC3140-Meyer-lec6 8

Execute MySQL Queries (SELECT) // set up and execute the MySQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ‘.mysql_error()); // print the results as an HTML table print " \n"; while ($row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) { echo "\t \n"; foreach ( $row as $item ) { echo "\t\t $item \n"; } echo "\t \n"; } echo " \n"; // free result mysql_free_result( $result ); CISC3140-Meyer-lec6 9

Database Functions Execute the query and store the result in a local variable: mysql_query() Parse the data read returned from the query as an array: mysql_fetch_array() Free the memory used by the query result: mysql_free_result() NOTE that if the result returned is a scalar and not an array, then only mysql_query() needs to be called and does not need to be followed by a call to mysql_fetch_array(). Finally, note the use of mysql_error() in the query function. CISC3140-Meyer-lec6 10

Other Handy Functions Here are a few of the more handy functions: int mysql_num_rows ( $result ) Returns the number of rows contained in a $result; this is relevant when the result returned from mysql_query() is an array and not a scalar int mysql_affected_rows ($conn) This function returns the number of rows that were affected by the most recently executed INSERT, UPDATE, REPLACE or DELETE query (i.e. if the number of rows you expected to be updated were actually updated) CISC3140-Meyer-lec6 11

Even more handy functions int mysql_insert_id ( $conn ) this function is used when inserting a row into a table that has an AUTO_INCREMENT ID field; the function returns the ID number that was generated string mysql_stat ( $conn ) This function returns a string containing information about the status of the current database connection CISC3140-Meyer-lec6 12

Sample Program <?php $conn = mysql_connect("localhost",“username",”password”) or die('Could not connect:'.mysql_error()); $db_found = mysql_select_db("pgrabowiec"); if ($db_found) { $query = "SELECT thename FROM friends"; $result = mysql_query($query) or die('Query failed: '.mysql_error()); print " \n"; while ($db_fields = mysql_fetch_assoc($result)) { print "\t ".$db_fields['thename']." “; } print " \n"; } else print "Database not found"; mysql_close($conn); ?> CISC3140-Meyer-lec6 13

You Can Do This!!! MySQL and PHP online documentation PHP Site ▫ W3c-Schools ▫ CISC3140-Meyer-lec6 14

Lab 4.0 – PHP & MySQL Available online Assumes you have completed the MySQL labs. Get started, if you can't finish it in class, then finish it over the weekend. Hook the pages that you create to the index page in your public_html folder. Check out the following site for help: CISC3140-Meyer-lec6 15