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 34• • •<strong>MySQL</strong> ViewsEven relatively simplistic data-driven applications rely on queries involving several tables. For instance,suppose you were charged with creating a human resources application and wanted to create aninterface that displays each employee’s name, e-mail address, total number of absences, and bonuses.The query might look like this:SELECT emp.employee_id, emp.firstname, emp.lastname, emp.email,COUNT(att.absence) AS absences, COUNT(att.vacation) AS vacation,SUM(comp.bonus) AS bonusFROM employees emp, attendance att, compensation compWHERE emp.employee_id = att.employee_idAND emp.employee_id = comp.employee_idGROUP BY emp.employee_id ASCORDER BY emp.lastname;Queries of this nature are enough to send shudders down one’s spine because of their size,particularly when they need to be repeated in several locations throughout the application. Another sideeffect of such queries is that they open up the possibility of someone inadvertently disclosing potentiallysensitive information. For instance, what if, in a moment of confusion, you accidentally insert thecolumn emp.ssn (the employee’s Social Security number, or SSN) into this query? This would result ineach employee’s SSN being displayed to anybody with the ability to review the query’s results. Yetanother side effect of such queries is that any third-party contractor assigned to creating similarinterfaces could potentially gain access to sensitive data, opening up the possibility of identity theft andcorporate espionage.What’s the alternative? After all, queries are essential to the development process, and unless youwant to become entangled in managing column-level privileges (see Chapter 29), it seems you’ll justhave to grin and bear it.Such inconveniences were long the case for <strong>MySQL</strong> users, until version 5 introduced a great featureknown as a view. Views offer a way to encapsulate queries much like the way a stored routine (seeChapter 32) serves as an alias for a set of commands. For example, you could create a view of thepreceding example query and execute it like this:SELECT * FROM employee_attendance_bonus_view;This chapter begins by briefly introducing the concept of views and the various advantages ofincorporating views into your development strategy. It then discusses <strong>MySQL</strong>’s view support, showingyou how to create, execute, and manage views. Finally, you’ll learn how to incorporate views into your<strong>PHP</strong>-driven web applications.659

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

Saved successfully!

Ooh no, something went wrong!