_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.

Slides:



Advertisements
Similar presentations
PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.
Advertisements

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.
Working with Tables 1 of 5. Working with Tables 2 of 5.
1 Chapter 12 Working With Access 2000 on the Internet.
PHP (2) – Functions, Arrays, Databases, and sessions.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Objectives Connect to MySQL from PHP
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Intermediate PHP & MySQL
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
About Dynamic Sites (Front End / Back End Implementations) by Janssen & Associates Affordable Website Solutions for Individuals and Small Businesses.
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.
PHP meets MySQL.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Analyzing Data For Effective Decision Making Chapter 3.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
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.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Attacking Data Stores Brad Stancel CSCE 813 Presentation 11/12/2012.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
Creating PHPs to Insert, Update, and Delete Data CS 320.
M1G Introduction to Database Development 5. Doing more with queries.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Module Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Lesson 4: Querying a Database. 2 Learning Objectives After studying this lesson, you will be able to:  Create, save, and run select queries  Set query.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
ASP.NET Programming with C# and SQL Server First Edition
Databases.
A Guide to SQL, Seventh Edition
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 4 Summary Query.
Server-Side Processing II
Presentation transcript:

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the book authors, 2002 PHP Bible Chapter 18: PHP Efficiency and Style

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition2  Wiley and the book authors, 2002 Summary Using database resources efficiently Making the database work for you Timestamping insertions and updates

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition3  Wiley and the book authors, 2002 PHP Efficiency and style This chapter is for people making database-enabled PHP web sites who suspect that they are doing things awkwardly or inefficiently Included are some tips and tricks for making things run faster and show some common ways that database systems can save you from writing unnecessary PHP code

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition4  Wiley and the book authors, 2002 Connections – Reduce, Reuse, Recycle One important thing to realize is that establishing an initial connection with a database is never a cheap operation  Unless your PHP script is doing some unusually computationally-intensive work, the overall database interaction will be the most time and resource-intensive part of your code, and it is frequently true that the establishment of a connection is the most expensive part of code that interacts with a database, even if the connection is only established once in serving the page One thing that is surprising is that with many database programs, it is possible to even retain the results from more than one query at once, even though only one connection has been opened If the overhead of opening new database connections is killing your performance, you may want to investigate opening persistent connections  Unlike regular db connections,these connections are not automatically killed when your page executes, but are saved in a pool for future use  The first time a script opens a connection in this manner, it is opened in the same was as with a regular db connection, the next script will get the same connection if the parameters of the new request are identical  To create a persistent connection to a MySQL db, use mysql_pconnect()

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition5  Wiley and the book authors, 2002 Make the database work for you As when writing code in a programming language, writing code that interacts with a database is an exercise in appropriate division of labor People who write programming languages and databases have agreed to automate, standardize, and optimize certain tasks that come up over and over again in programming, so that programmers don't have to constantly reinvent the wheel when making their individual applications As with sorting and other common tasks used within programming, if you can find a function that can perform that task, use it instead of writing your own. It will generally be much faster than what you can write and it will almost always be easier

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition6  Wiley and the book authors, 2002 Make the database work for you (cont.) In particular, any searching or sorting of the contents of a database is best done within that database, rather than by your own code In this example, the query returns the entire dataset of the author table, instead of just what is needed for the application function print_first_name ($lastname, $dbconnection) { $query = 'select firstname, lastname from author'; $result_id = mysql_query($query, $dbconnection) or die ('can\'t query'); while ($row = mysql_fetch_array($result_id)) if ($row['lastname'] == $lastname) print('The first name is '.$row['firstname']); } When this function is executed, it will print out every associated firstname for the lastname that is passed in as an argument

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition7  Wiley and the book authors, 2002 Make the database work for you (cont.) The problem is that we don't need to grab all the data in this table, pull it through the connection, and then pick and choose from it on our side of the pipe Instead, we should restrict the query with a WHERE clause function print_first_name ($lastname, $dbconnection) { $query = 'select firstname from author WHERE (lastname="'.$lastname.'")'; $result_id = mysql_query($query, $dbconnection) or die ('can\'t query'); while ($row = mysql_fetch_assoc($result_id)) print('The first name is '.$row['firstname']); } The WHERE clause ensures that only the rows we care about are selected in the first place Not only does this cut down on the data passed over the SQL connection, the code used to locate the correct rows on the database side is almost certainly quicker than the previous PHP code

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition8  Wiley and the book authors, 2002 Sorting and aggregating Exactly the same argument applies if you find yourself writing code to sort results that have been returned from your database, or to count, average, or otherwise aggregate those results In general, the ORDER BY syntax in SQL will allow you to presort your retrieved rows by any prioritized list of columns in the query, and that sort will probably be more efficient than either homegrown code or the PHP array-sorting functions Similarly, rather than looping through DB rows to count, sum, or average a value, investigate whether the syntax of your particular database's flavor of SQL supports the GROUP BY construct, and in-query functions like count, sum, and average In calculated fields especially, you can use the AS keyword to specify/change the field name that gets returned in the query $sql = 'SELECT count(ID) AS ID_Count FROM author';  Will return a one-field (ID_Count) record which counts the occurrences of ID in the table author $sql = 'SELECT sum(charge) AS Charge_Sum FROM order_details GROUP BY order_id';  Will return a record for each unique order_id in order_details calculating the sum of the charge field within each group $sql = 'SELECT ID FROM author ORDER BY ID LIMIT 1'; $sql = 'SELECT MIN(ID) AS Min_ID FROM author';  Both of these queries will return the minimum author ID from the author table, but the second query will execute faster since once the minimum ID is found, it will not have to re-order the remaining records

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition9  Wiley and the book authors, 2002 Creating date and time fields It is very common to want to associate a date and/or time with a row's data (e.g. your table rows may represent requests made by your Web users and you want to store the date and time that request was made) One way to accomplish this task would be to include a string that represents the desired date in a format parsable by your database This will work as long as the format you are attempting to store is readable as a date by your db This also requires you to query the system to find the current date and time before you can construct the appropriate string This is unnecessary in most databases since they have a current-date function $sql = 'UPDATE mytable SET mydate = now() WHERE (record_id = 1); This can also be supplanted by the usage of certain data formats in the creation of your tables in your database  MySQL includes a data type called TIMESTAMP which will automatically store the current date and time that a record is added or updated if the field in the UPDATE or INSERT query is not populated or not specified