Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.

Slides:



Advertisements
Similar presentations
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Advertisements

Web Database Programming Connecting Database to Web.
By Brian Vees.  SQL Injection  Username Enumeration  Cross Site Scripting (XSS)  Remote Code Execution  String Formatting Vulnerabilities.
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.
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.
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.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Chapter 14 Database Access through the Web. © 2006 Pearson Addison-Wesley. All rights reserved Relational Databases - A database is a collection.
© 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:
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)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Database Access Through the Web.
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 Bible, 2 nd Edition1  Wiley and the.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
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.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
INTERNET APPLICATION DEVELOPMENT Practical on Sessions.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
PHP with MySQL 1.
Website Development with PHP and MySQL Saving Data.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
Internet Information Systems Writing to Databases and Amending Data.
MySQL Database Connection
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
PHP - MySQL Connection 8 March 2006 Adina Crainiceanu IT420: Database Management and Organization.
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
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.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on.
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.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
PHP and SQL Server: Connection IST2101. Typical web application interaction (php, jsp…) database drivers 2IST210.
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.
PHP Error Handling & Reporting. Error Handling Never allow a default error message or error number returned by the mysql_error() and mysql_errno() functions.
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.
PHP with MYSQL Please use speaker notes for additional information!
13 – PHP MySQL Connection Informatics Department Parahyangan Catholic University.
Radoslav Georgiev Telerik Corporation
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
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 ©
Intro to SQL Programming Techniques
PHP and MySQL.
Web Systems Development (CSC-215)
PHP: Combo box FdSc Module 109 Server side scripting and
Web Programming– UFCFB Lecture
Database Access with PHP and MySQL
Presentation transcript:

Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002

PHP for Database Access Connect to the MySQL server –$ connection = mysql_connect("localhost", $username, $password); Access the database –mysql_select_db("winestore", $connection); Perform SQL operations Disconnect from the server –mysql_close($connection);

Error Handling All mysql_ functions return NULL (or false) if they fail. Several functions are helpful in graceful failure –die(string) - halts and displays the string –mysql_errno() - returns number of error –mysql_error() - returns text of error

Error Handling examples if (!($connection = mysql_connect("localhost",$name,$passwd))) die("Could not connect"); function showerror() { die("Error ". mysql_errno(). " : ". mysql_error()); } if (!(mysql_select_db("winestor", $connection))) showerror();

Building a Query Directly –$ query = 'select * from wines'; Using input information –$ winery = $_POST['winery']; –$query = “select * from wines where winery=$winery”;

Running a Query mysql_query returns a result handle $result = mysql_query($query, $connection) mysql_num_rows indicates the number of rows returned $num_rows = mysql_num_rows($result) mysql_fetch_array creates array/hash of result For ($n=0; $n<$num_rows;$n++) $row = mysql_fetch_array($result)

Result of fetch_array Contains both numeric and index tags Values are duplicated Example: –Query: select surname, city from customers; –Row: ( 0=>'Walker', ‘surname’=>'Walker', 1=>'Kent', 'city'=>'Kent' );

Printing the Complete Row By number for ($i=0; $i<mysql_num_fields($result); $i++) echo $row[$i]. " "; By field echo $row['surname']. ' '. $row['city'];

Avoiding Special Characters When building HTML, characters such as '&' in the data can cause problems Function htmlspecialchars() replaces all such characters with HTML escapes such as & print( htmlspecialchars($row['surname']. ' '. $row['city']);

Special Characters in Input The same problem exists with special characters in input (e.g. ' ) PHP switch magic_quotes_gpc (default on) inserts backslashes before single & double quotes, backslashes and NULL characters in input data (from GET, PUT and cookie data) Use stripslashes() to remove the slashes Use addslashes() to add the slashes if magic_quotes_gpc is off

Avoid Dangerous User Input Passing user input to other programs opens the door to exploits –Eg. exec("/usr/bin/cal $input") –Generates a calendar (/usr/bin/cal 2004) –But a malevolent user might send '2004 ; cat /etc/passwd' or '2004 ; rm *' Overlong inputs can also cause problems Always clean input –$input = escapeshellcmd($input); –$input = substr($input,$maxlength);

PHP / Form in one Document Combine the original form with the PHP document that processes data if empty($regionName)) { //parameter provided? //produce the } else { //run the query using data from $_GET or $_POST }

Inserting Into a Database Collect data from a form Validate data (JavaScript, PHP or both) Create a query $query = "insert into customer set cust_id = NULL, ". "surname =\"". $surname."\"" … Run the query mysql_query($query, $db);

Updating a Database Query to find item to update Present old information Collect new information Validate Construct and run the update query