11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoC H A P T E R 35• • •Practical Database QueriesThe last several chapters served as an introduction to numerous concepts regarding using <strong>PHP</strong> and<strong>MySQL</strong> together to retrieve and manipulate data. This chapter expands your knowledge, demonstratingseveral challenges that you’re bound to repeatedly encounter while creating database-driven webapplications. In particular, you’ll learn more about the following concepts:• Tabular output: Listing query results in an easily readable format is one of themost commonplace tasks you’ll deal with when building database-drivenapplications. This chapter explains how to programmatically create these listings.• Sorting tabular output: Often, query results are ordered in a default fashion, byproduct name, for example. But what if the user would like to reorder the resultsusing some other criteria, such as price? You’ll learn how to provide table-sortingmechanisms that let the user search on any column.• Subqueries: Even simple data-driven applications often require queries to work withmultiple tables, typically using joins. However, as you’ll learn, many of theseoperations can also be accomplished with the arguably much more intuitive subquery.• Cursors: Operating in a fashion similar to an array pointer, a cursor (a feature newto <strong>MySQL</strong> 5.0) gives you the ability to swiftly navigate database result sets. In thischapter you’ll learn how to use cursors to streamline your code.• Paged results: Database tables can consist of thousands, even millions, of records.When large result sets are retrieved, it often makes sense to separate these resultsacross several pages and provide the user with a mechanism to navigate back andforth between these pages. This chapter explains how to do so.Sample DataMany of the examples found throughout much of this chapter are based upon the products and salestables, presented here:CREATE TABLE products (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,product_id VARCHAR(8) NOT NULL,name VARCHAR(25) NOT NULL,price DECIMAL(5,2) NOT NULL,description MEDIUMTEXT NOT NULL);673

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!