7- PHP and MySQL queries

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

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

Server side scripting and databases

A Brief Introduction to MySQL

Web Programming Step by Step

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

2- Forms and JavaScript Course: Developing web- based applica<ons

The following steps detail how to prepare your database.

8- Management of large data volumes

Using Cloud Databases in the Cloud Control Panel By J.R. Arredondo

Download: Server-side technologies. WAMP (Windows), MAMP (Mac),

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

DIPLOMA IN WEBDEVELOPMENT

Chapter 2: Interactive Web Applications

CPE111 COMPUTER EXPLORATION

Concepts Design Basics Command-line MySQL Security Loophole

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

Web Programming with PHP 5. The right tool for the right job.

ICADBS504A Integrate database with a website

Writing Scripts with PHP s PEAR DB Module

Web Development using PHP (WD_PHP) Duration 1.5 months

SQL Injection. Blossom Hands-on exercises for computer forensics and security

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

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

PHP Tutorial From beginner to master

ICAB4136B Use structured query language to create database structures and manipulate data

Accessing External Databases from Mobile Applications

Supercharge your MySQL application performance with Cloud Databases

Database Extension 1.5 ez Publish Extension Manual

MYSQL DATABASE ACCESS WITH PHP

Unlocking Hadoop for Your Rela4onal DB. Kathleen Technical Account Manager, Cloudera Sqoop PMC Member BigData.

Application note: Connecting the to a Database

Python. Data bases. Marcin Młotkowski. 8th May, 2013

HTSQL is a comprehensive navigational query language for relational databases.

Create dynamic sites with PHP & MySQL

Other Language Types CMSC 330: Organization of Programming Languages

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

COURSE OUTLINE UCLA EXTENSI ON MGMNT X

Database: SQL, MySQL

DbSchema Tutorial with Introduction in SQL Databases

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

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

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

Manage cloud infrastructures using Zend Framework

Persistent Stored Modules (Stored Procedures) : PSM

edoc Document Generation Suite

MySQL Quick Start Guide

How To Use Postgresql With Foreign Data In A Foreign Server In A Multi Threaded Database (Postgres)

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

Monitoreo de Bases de Datos

Oracle Database 10g Express

Connect to MySQL or Microsoft SQL Server using R

Role Based Access Control. Using PHP Sessions

Designing for Dynamic Content

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

A basic create statement for a simple student table would look like the following.

Andrew Moore Amsterdam 2015

Performance Implications of Various Cursor Types in Microsoft SQL Server. By: Edward Whalen Performance Tuning Corporation

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

Release: 1. ICADBS412A Build a database

MySQL quick start guide

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Chapter 22 Database: SQL, MySQL,

Equipment Room Database and Web-Based Inventory Management

Using ODBC with MDaemon 6.5

What s New in Delphi for PHP 2.0. New and Enhanced IDE Features. Form Designer

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Computing with large data sets

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

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

SQL Server Instance-Level Benchmarks with DVDStore

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Vendor: Crystal Decisions Product: Crystal Reports and Crystal Enterprise

Automated vulnerability scanning and exploitation

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:

VIDEO CONFERENCING TOOL

CSCI110 Exercise 4: Database - MySQL

MySQL Quick Start Guide

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE CISY 233 INTRODUCTION TO PHP

Technology Foundations. Conan C. Albrecht, Ph.D.

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

Relational Database Basics Review

Advanced Web Security, Lab

Intro to Databases. ACM Webmonkeys 2011

Facebook Twitter YouTube Google Plus Website

XMailer Reference Guide

Server-side scripting with PHP4

Online shopping store

Transcription:

7- PHP and MySQL queries Course: Cris*na Puente, Rafael Palacios 2010-

1 Introduc*on

Introduc?on PHP includes libraries for communica*ng with several databases: MySQL (OpenSource, the use selected for this course) DBM (Berkeley) Informix MS SQL (MicrosoO) Oracle 8 PostgreSQL (Berkeley open source) Sybase 3

Introduc?on Who does it work? GET prueba.php 4

Example of PHP program 1. Open connec*on with database server 2. Select database 3. Build the query 4. Execute the query 5. Loop to display results row by row 6. Close connec*on 5

2 Open database connec*on

Database connec?on mysql_connect(), sentence including three parameters: host name, user name, and password. In local modo use "localhost" (localhost = 127.0.0.1) <?php $conn = mysql_connect("localhost","mysql_user","mysql_pwd"); if (!$conn) { die('error connecting to database server'); } /* Code goes here*/ mysql_close($conn);?> 7

Database connec?on It is possible to use variables and develop func*ons <?php function Connect() { $host="localhost"; $user="mysql_user"; $pass="shhh"; $conn = mysql_connect($host,$user,$pass); if (!$conn) { die('connection Error'); } return $conn; } Access.php 8

Selección de la base de datos Los ficheros que accedan a la base de datos incluyen la función. <?php include("access.php"); $conn=connect(); if (!mysql_select_db("testing",$conn)) { printf("error: %s\n",mysql_error()); mysql_close($conn); die("error selecting database.\n"); } /* Code goes here*/ mysql_close($conn); 9

3 Queries

Query example Queries are executed with mysql_query. It is recommended to check the error $query="select name, lastname FROM users WHERE salary<10000;"; $result=mysql_query($query,$conn); if (!$result) { printf("error in query: ".mysql_error()); mysql_close($conn); } exit; 11

Query example mysql_fetch_array stores the result of the query into one array containing all the informa*on of one row. Array elements use the same names as the tables. $row=mysql_fetch_array($result); while ($row) { printf("name: {$row['name']} {$row['lastname']}<br>"); $row=mysql_fetch_array($result); } mysql_free_result($result); 12

Summary of PHP func?ons to access MySQL mysql_connect() :Connects to database server. mysql_select_db() :Selects database. mysql_query() : Excutes SQL query. mysql_num_rows($result) : Number of resul*ng registers mysql_fetch_array($result) : Convert result into array variable mysql_free_result($result) : Free memory mysql_close() : Closes connec*on. mysql_error() : Displays latest error message mysql_real_escape_string() : Obtain secure string (see below) 13

4 Forms and security aspects

Form- based query 1. Design your page using any HTML editor 2. Convert into PHP and insert database code 3. Obtain and protect form data 4. Build the query 5. Create output 15

Structure of PHP file Initial HTML code PHP Query final HTML code 16

Building the query if (isset($_get['salary'])) { $salary=mysql_real_escape_string($_get['salary']); //important $query='select firstname, lastname, salary FROM users WHERE salary>={$salary};'; } else { /* No filter */ $query='select firstname, lastname, salary FROM users;'; } $result=mysql_query($query,$conn); if (!$result) { die('query error '.mysql_error()); } $row=mysql_fetch_array($result); while ($row) { printf("<tr>\n"); printf("<td>{$row['firstname']}</td><td>{$row['lastname']}</td>"); printf("<td>{$row['salary']}</td></tr>"); $row=mysql_fetch_array($result); } mysql_free_result($result); 17

Security aspects Why is mysql_real_escape_string important? 10000 query='select name FROM users WHERE salary<=10000;' 10000; drop table users; query='select name FROM users WHERE salary<=10000; drop table users' 18

Alberto Aguilera 25 28015 Madrid Tel +34 91 542 28 00 Fax + 34 91 542 31 76 Iwww.icai.upcomillas.es www.upcomillas.es