Connecting to a Database Using PHP. Prof. Jim Whitehead CMPS 183, Spring 2006 May 15, 2006



Similar documents
7- PHP and MySQL queries

A Brief Introduction to MySQL

Server side scripting and databases

Web Programming Step by Step

A table is a collection of related data entries and it consists of columns and rows.

Database Extension 1.5 ez Publish Extension Manual

The following steps detail how to prepare your database.

MYSQL DATABASE ACCESS WITH PHP

Security and Control Issues within Relational Databases

Databases and SQL. The Bioinformatics Lab SS Wiki topic 10. Tikira Temu. 04. June 2013

MySQL Security for Security Audits

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Using Apache Derby in the real world

MySQL Quick Start Guide

Supercharge your MySQL application performance with Cloud Databases

Zend Framework Database Access

Eurobackup PRO: Configuration Best Practices

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

SQL Injection Vulnerabilities in Desktop Applications

Package sjdbc. R topics documented: February 20, 2015

"SQL Database Professional " module PRINTED MANUAL

CSCI110 Exercise 4: Database - MySQL

Final Design Document for Practicum Chat Program

MySQL Quick Start Guide

AVALANCHE MC 5.3 AND DATABASE MANAGEMENT SYSTEMS

Install Guide - Hosted

ORACLE DATABASE SECURITY. Keywords: data security, password administration, Oracle HTTP Server, OracleAS, access control.

WordPress Security Scan Configuration

HP OO 10.X - SiteScope Monitoring Templates

How To Let A Lecturer Know If Someone Is At A Lecture Or If They Are At A Guesthouse

Designing for Dynamic Content

Video Administration Backup and Restore Procedures

SQL Injection. The ability to inject SQL commands into the database engine through an existing application

Accessing Your Database with JMP 10 JMP Discovery Conference 2012 Brian Corcoran SAS Institute

ADO and SQL Server Security

Oracle Database Security

MatriXay Database Vulnerability Scanner V3.0

Technical. Overview. ~ a ~ irods version 4.x

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Chapter 2: Interactive Web Applications

Thick Client Application Security

SQL Injection January 23, 2013

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

FileMaker Security Guide The Key to Securing Your Apps

Release: 1. ICADBS601A Build a data warehouse

Connecting to your Database!... 3

1. Building Testing Environment

Smart Home Security System Based on Microcontroller Using Internet and Android Smartphone

How to configure your Desktop Computer and Mobile Devices post migrating to Microsoft Office 365

PHP Tutorial From beginner to master

Configuring Nex-Gen Web Load Balancer

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Synchronization Agent Configuration Guide

Automate Your BI Administration to Save Millions with Command Manager and System Manager

Various Load Testing Tools

Ordering Offsite Backups

1. Please login to the Own Web Now Support Portal ( with your address and a password.

Connect to MySQL or Microsoft SQL Server using R

Perceptive Intelligent Capture Solution Configration Manager

Application note: Connecting the to a Database

DIPLOMA IN WEBDEVELOPMENT

1. Open the preferences screen by opening the Mail menu and selecting Preferences...

Software Architecture Document

Web Development using PHP (WD_PHP) Duration 1.5 months

To use MySQL effectively, you need to learn the syntax of a new language and grow

Web Application Security Part 1

Role Based Access Control. Using PHP Sessions

Welcome to Collage (Draft v0.1)

MySQL for Beginners Ed 3

Installation Tutorial Script: The Real Estate Script. 1. Please login to download script. On PHP Classifieds Script web site.

Oracle Database 10g: Introduction to SQL

Eylean server deployment guide

Real SQL Programming 1

WSO2 Business Process Server Clustering Guide for 3.2.0

Configuring and Monitoring Database Servers

ESET Secure Authentication Java SDK

HIPAA Compliance Use Case

External Vulnerability Assessment. -Technical Summary- ABC ORGANIZATION

IBM Campaign and IBM Silverpop Engage Version 1 Release 2 August 31, Integration Guide IBM

Intunex Oy Skillhive Service Description 1 / 6

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

Oracle EXAM - 1Z Oracle Database 11g Security Essentials. Buy Full Product.

Seamless Web Data Entry for SAS Applications D.J. Penix, Pinnacle Solutions, Indianapolis, IN

Web Application Report

news from Tom Bacon about Monday's lecture

Database Security. Principle of Least Privilege. DBMS Security. IT420: Database Management and Organization. Database Security.

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

ICADBS504A Integrate database with a website

Advanced Web Security, Lab

IT360: Applied Database Systems. Database Security. Kroenke: Ch 9, pg PHP and MySQL: Ch 9, pg

FileMaker 12. ODBC and JDBC Guide

TrueSight Operations Management Monitoring Studio

Oracle Database 11g Security Essentials

How-To: MySQL as a linked server in MS SQL Server

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

DBMS / Business Intelligence, SQL Server

Transcription:

Connecting to a Database Using PHP Prof. Jim Whitehead CMPS 183, Spring 2006 May 15, 2006

Rationale Most Web applications: Retrieve information from a database to alter their on-screen display Store user data such as orders, tracking, address, credit card, etc. in a database Permits them to adapt to individual users, and provide fresh, changing content

PHP: Built-in Database Access PHP provides built-in database connectivity for a wide range of databases MySQL, PostgreSQL, Oracle, Berkeley DB, Informix, msql, Lotus Notes, and more Starting support for a specific database may involve PHP configuration steps Another advantage of using a programming language that has been designed for the creation of web apps. Support for each database is described in the PHP manual at: http://www.php.net/manual/en/

MySQL and PHP Architecture diagram Logical Web Server OS proc. 1 Thread 1 OS proc. n Thread 1 Logical Database (MySQL) Thread 2 Thread n Thread 2 Thread n

Connecting to MySQL To connect to a database, need to create a connection At lowest level, this is a network connection Involves a login sequence (username/password) Since this is a relatively expensive step, web application environments: Share connections Have multiple connections Whether, and how many, are typical configuration items. In MySQL: Allow_persistent: whether to allow persistent connections Max_persistent: the maximum number of persistent connections Max_links: max number of connections, persistent and not Connection_timeout: how long the persistent connection is left open Can also use SSL to encrypt connection

High-Level Process of Using MySQL from PHP Create a database connection Select database you wish to use Perform a SQL query Do some processing on query results Close database connection

Creating Database Connection Use either mysql_connect or mysql_pconnect to create database connection mysql_connect: connection is closed at end of script (end of page) mysql_pconnect: creates persistent connection connection remains even after end of the page Parameters Server hostname of server Username username on the database Password password on the database New Link (mysql_connect only) reuse database connection created by previous call to mysql_connect Client Flags MYSQL_CLIENT_SSL :: Use SSL MYSQL_CLIENT_COMPRESS :: Compress data sent to MySQL

Security Note Username and password fields imply that database password is sitting there in the source code If someone gains access to source code, can compromise the database Servers are sometimes configured to view PHP source code when a resource is requested with.phps instead of.php One approach to avoid this: put this information in Web server config. File Then ensure the Web server config. file is not externally accessible

Selecting a Database mysql_select_db() Pass it the database name Related: mysql_list_dbs() List databases available Mysql_list_tables() List database tables available

Perform SQL Query Create query string $query = SQL formatted string $query = SELECT * FROM table Submit query to database for processing $result = mysql_query($query); For UPDATE, DELETE, DROP, etc, returns TRUE or FALSE For SELECT, SHOW, DESCRIBE or EXPLAIN, $result is an identifier for the results, and does not contain the results themselves $result is called a resource in this case A result of FALSE indicates an error If there is an error mysql_error() returns error string from last MySQL call

Process Results Many functions exist to work with database results mysql_num_rows() Number of rows in the result set Useful for iterating over result set mysql_fetch_array() Returns a result row as an array Can be associative or numeric or both (default) $row = mysql_fetch_array($result); $row[ column name ] :: value comes from database row with specified column name $row[0] :: value comes from first field in result set

Process Results Loop Easy loop for processing results: $result = mysql_query($qstring); $num_rows = mysql_num_rows($result); for ($i=0; $i<$num_rows; $i++) { $row = mysql_fetch_array($result); // take action on database results here }

Closing Database Connection mysql_close() Closes database connection Only works for connections opened with mysql_connect() Connections opened with mysql_pconnect() ignore this call Often not necessary to call this, as connections created by mysql_connect are closed at the end of the script anyway