25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

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

144 ” Database Programming<br />

SQL supports a number of data types, which provide a greater degree of flexibility<br />

than <strong>PHP</strong> in how the data is stored and represented. For example, numeric values<br />

can be stored using a variety of types:<br />

int or integer<br />

smallint<br />

real<br />

float<br />

Signed integer number, 32 bits in length.<br />

Signed integer number, 16 bits in length.<br />

Signed floating-point number, 32 bits in length.<br />

Signed floating-point number, 64 bits in length.<br />

To these, most database systems add their own, non-standard variants—for example,<br />

MySQL supports a data type called tinyint, which is represented as a one-byte<br />

signed integer number. Clearly, all of these data types are converted into either integers<br />

or floating-point numbers when they are retrieved into a <strong>PHP</strong> variable, which is<br />

not normally a problem. However, you need to be aware of the precision and range<br />

of each data type when you write data from a <strong>PHP</strong> script into a database table, since<br />

it’s quite possible that you will cause an overflow (which a database system should<br />

at least report as a warning).<br />

This is even more apparent—and, generally, more common—when you deal with<br />

string data types. SQL-92 defines two string types:<br />

char<br />

varchar<br />

Fixed-length character string.<br />

Variable-length character string.<br />

The only difference between these two data types is in the fact that a char string<br />

will always have a fixed length, regardless of how many characters it contains (the<br />

string is usually padded with spaces to the column’s length). In both cases, however,<br />

a string column must be given a length (usually between 1 and 255 characters, although<br />

some database systems do not follow this rule), which means that any string<br />

coming from <strong>PHP</strong>, where it can have an arbitrary length, can be truncated, usually<br />

without even a warning, thus resulting in the loss of data.<br />

Most database systems also define an arbitrary-length character data type (usually<br />

called text) that most closely resembles <strong>PHP</strong>’s strings. However, this data type usually<br />

comes with a number of strings attached (such as a maximum allowed length<br />

and severe limitations on search and indexing capabilities). Therefore, you will still<br />

be forced to use char and (more likely) varchar, with all of their limitations.<br />

Strings in SQL are enclosed by single quotation strings:<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

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

Saved successfully!

Ooh no, something went wrong!