25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

150 ” Database Programming<br />

DELETE FROM book WHERE isbn = ’0812550706’;<br />

i<br />

Retrieving Data<br />

As we mentioned earlier, relational database are biased toward read operations;<br />

therefore, it follows that the most common SQL statement is designed to extract data<br />

from a database.<br />

To retrieve data from any SQL database engine, you must use a SELECT statement;<br />

SELECT statements range from very simple to incredibly complex, depending on your<br />

needs. Its most basic form, however, is simple and easy to use:<br />

SELECT * FROM book;<br />

The statement begins with the verb or action keyword SELECT, followed by a commaseparated<br />

list of columns to include in the dataset retrieved. In this case, we use the<br />

special identifier * , which is equivalent to extracting all of the columns available in<br />

the dataset. Following the list of columns is the keyword FROM, which is itself followed<br />

by a comma-separated list of tables. This statement retrieves data from only one<br />

table, the book table.<br />

The format in which the dataset is returned to <strong>PHP</strong> by the database system depends<br />

largely on the system itself and on the extension you are using to access it; for example,<br />

the “traditional” MySQL library returns datasets as resources from which you can<br />

extract individual rows in the form of arrays. Newer libraries, on the other hand, tend<br />

to encapsulate result sets in objects.<br />

You will rarely need to gain access to all of the records in a table—after all, relational<br />

databases are all about organizing data and making it easily searchable. Therefore,<br />

you will most often find yourself limiting the rows returned by a SELECT statement<br />

using a WHERE clause. For example, for the book table, you may wish to retrieve<br />

all books written by a specific author. This is possible using WHERE.<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

SELECT * FROM book WHERE author = ’Ray Bradbury’;

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

Saved successfully!

Ooh no, something went wrong!