PHP 5 + MySQL 5 A Perfect 10. Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect 10 1. mysqli extension i is for improved! All new MySQL extension for PHP.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
LIS651 lecture 3 taming PHP Thomas Krichel
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Why is that LOV in the screen not returning me desired value?
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
PHP (2) – Functions, Arrays, Databases, and sessions.
LCT2506 Internet 2 Further SQL Stored Procedures.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
INTERNET APPLICATION DEVELOPMENT For More visit:
Databases with PHP A quick introduction. Y’all know SQL and Databases  You put data in  You get data out  You can do processing on it very easily 
LIS651 lecture 7 PHP mySQL Thomas Krichel
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
PHP Data Objects Layer (PDO) Ilia Alshanetsky. What is PDO Common interface to any number of database systems. Common interface to any number of database.
Sayed Ahmed Computer Engineering, BUET, Bangladesh MSC, Computer Science, U of Manitoba, Canada
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
What is MySQLi? Since the mid-90s, Mysql extension has served as the major bridge between PHP and MySQL. Although it has performed its duty quite well,
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP Part 2.
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
PHP 5 Sucks! PHP 5 Rocks! Adam Trachtenberg eBay Technical Evangelist
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Prof Frankl, Spring 2008CS Polytechnic University 1 Overview of Web database applications with PHP.
Improving Database Performance Derrick Rapley
WEB SECURITY WEEK 2 Computer Security Group University of Texas at Dallas.
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
CHAPTER 10 PHP MySQL Database
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
DATABASES.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Oracle Query VBA Tool (OQVT)
PDOStatement Named Placeholders CIT336 - Connor Wiseman cit336.saveandquit.net/presentation.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL Injection By Wenonah Abadilla. Topics What is SQL What is SQL Injection Damn Vulnerable Web App SQLI Demo Prepared Statements.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Web Systems & Technologies
CS3220 Web and Internet Programming Database Access with JDBC
Introduction to Dynamic Web Programming
Web Technologies IT230 Dr Mohamed Habib.
Unix System Administration
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Web Systems Development (CSC-215)
Lecture 5: Functions and Parameters
Tutorial 6 PHP & MySQL Li Xu
MySQL Web Application Connecting to a MySQL database
PHP Forms and Databases.
Dynamic SQL Konstantin Osipov, MySQL AB.
CS3220 Web and Internet Programming Database Access with JDBC
SQL Injection Attack.
Presentation transcript:

PHP 5 + MySQL 5 A Perfect 10

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect mysqli extension i is for improved! All new MySQL extension for PHP 5 Result of –New binary client protocol in MySQL 4.1 –Old mysql extension showing age Biggest change is that database handle is now mandatory and is the first argument

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect mysqli extension $db = mysqli_connect($server, $user, $password, "users"); $r = mysqli_query($db, "SELECT user FROM users"); while ($row = mysqli_fetch_assoc($r)) { print $row['user']; } mysqli_free_result($r); mysqli_close($db);

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Object-Oriented Interface Create a MySQL object! No real advantages over the procedural interface, except that objects are inherently cooler than functions. Actually, there are a few neat things you can do by subclassing: –Create specialized classes –Redefine methods

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Object-Oriented Interface $db = new mysqli($server, $user, $password, "users"); $r = $db->query("SELECT user FROM users"); while ($row = $r->fetch_assoc()) { print $row['user']; } $r->free_result(); unset($db);

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Prepared Statements Define a query “template” Faster for MySQL to execute Send less data Defense against SQL injection attacks

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Prepared Statements $db = mysqli_connect($server, $user, $password, 'stocks'); $sql = 'SELECT price FROM stocks WHERE ticker = ?'; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt, $sql)) { // More to come… }

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Bound Parameters Map PHP variables with MySQL fields Works with stored procedures Can bind for both input and output Watch your variable scope

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Bound Parameters $ticker = 'EBAY'; if (mysqli_stmt_prepare($stmt, $sql)) { mysqli_stmt_bind_param($stmt, 's', $ticker); mysqli_stmt_bind_result($stmt, $price); mysqli_stmt_execute($stmt); print "$ticker trades at $price\n"; } EBAY trades at 75.01

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Bound Parameters $ticker = 'EBAY'; $stmt = $db->stmt_init(); if ($stmt->prepare($sql)) { $stmt->bind_param('s', $ticker); $stmt->bind_result($price); $stmt->execute(); print "$ticker trades at $price\n"; } EBAY trades at 75.01

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect SSL Connections Encrypt the connection between PHP and MySQL Slows things down Useful when you don’t control the path between the client application and MySQL Somewhat painful to set up if you’re an SSL novice

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect SSL Connections $db = mysqli_init(); mysqli_ssl_set($db, '/usr/local/mysql/server-key.pem', '/usr/local/mysql/server-cert.pem', '/usr/local/mysql/cacert.pem', NULL, NULL); mysqli_real_connect($db, 'external.example.org', 'ssl-user', 'password', 'database');

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect SSL Connections $db = mysqli_init(); mysqli_options($db, MYSQLI_READ_DEFAULT_FILE, '/etc/my.cnf'); mysqli_real_connect($db, 'external.example.org', 'ssl- user', 'password', 'database');

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Multi-Query Statements Send multiple SQL queries all at once Super useful for phpMyAdmin Increases the danger of SQL injection attacks Requires special set of functions –Forcibly disabled in mysqli_query() More work to iterate; made easier by using an Iterator ™

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Multi-Query Statements if (mysqli_multi_query($db, $query)) { do { if ($r = mysqli_store_result($db)) { while ($row = mysqli_fetch_row($r)) { print "$row[0]\n"; } mysqli_free_result($result); } } while (mysqli_next_result($db)); }

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Multi-Query Statements $it = new MySQLiQueryIterator($db, $query); foreach ($it as $r) { if ($r) { while ($row = mysqli_fetch_row($r)) { print "$row[0]\n"; }

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Subselects New in MySQL 4.1 Run a query within a query Makes it faster and easier to filter data –Places work inside MySQL instead of PHP Could often by “worked around” using a self-join, but not always Know what your query will return –One row ( = ) or many ( IN() )?

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Subselects mysql> SELECT speaker FROM speakers WHERE topic = (SELECT topic FROM speakers WHERE speaker = 'Adam Trachtenberg'); Better hope I’m not talking on multiple subjects

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Subselects mysql> SELECT speaker FROM speakers WHERE topic IN (SELECT topic FROM speakers WHERE speaker = 'Adam Trachtenberg');

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Character Sets New in MySQL 4.1 Store data using different character sets Collate data using different character sets Important when –You want a case-insensitive sort –Different cultures place the same letter in different positions in their alphabets What to do with –n vs ñ –u vs ü

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Character Sets Four records 1.Muffler 2.Müller 3.MX Systems 4.MySQL mysql> SELECT X FROM T ORDER BY X COLLATE collation_name; latin1_swedish_cilatin1_german1_cilatin1_german2_ci Muffler Müller MX SystemsMüllerMuffler MüllerMX Systems MySQL

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Stored Procedures New in MySQL 5.0 Sequence of SQL statements stored on your MySQL server Make request with set of parameters, get back chunk of relatively complete data Works regardless of client language Speedier than even prepared statements More secure (can wall off access to tables except through pre-defined procedures) A work in progress…

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Stored Procedures mysql> CREATE PRODCEDURE getNumberOfSpeakers (OUT n INT) BEGIN SELECT COUNT(*) INTO n FROM speakers; END mysql> CALL mysql>

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Cursors New in MySQL 5.0 CURrent Set of RecordsS Lets you refer to the results of a SELECT statement on the server Works within stored procedures and functions Still fairly limited. Just a test.

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Cursors mysql> DECLARE speakers CURSOR FOR SELECT speaker, topic FROM speakers; mysql> OPEN speakers; mysql> FETCH speakers INTO s, t; mysql> CLOSE speakers;

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Views New in MySQL 5.0 Let you create a “virtual” table based on SQL queries CREATE VIEW view AS SELECT... SELECT statement can include JOIN s You can now refer to “view” as if it was a real table: SELECT * FROM view WHERE... Changing rows in the view alters the data back in the original table.

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect Next Year: Go to Eleven NIGEL: What we do is if we need that extra...push over the cliff...you know what we do? MARTY: Put it up to eleven. NIGEL: Eleven. Exactly. One louder. MARTY: Why don't you just make ten louder and make ten be the top... number...and make that a little louder? NIGEL:...these go to eleven.

Adam Trachtenberg PHP 5 + MySQL 5 = A Perfect 10 Shameless Plug: PHP 5, MySQL 4.0, and 4.1 New mysqli extension Everything covered here, but in greater detail. (Except 5.0) How to migrate –From PHP 4 / mysql / MySQL 4.0 –To PHP 5 / mysqli / MySQL 4.1