PHP199

From mi-linux
Jump to navigationJump to search

Viewing contents of array

A good tip for checking the information returned from the database is to use <pre> and print_r() which outputs arrays in a more readable form...

e.g.

Presuming the following SQL was used on the comics database...

SELECT title,author FROM comics LIMIT 1

This would return the title and author details for the first record in the table.

If you then used mysql_fetch_array() to get the first record and output it's contents wrapped in <pre> you get a nicely formatted view of the array and it's contents.

<?
// Database connection here...

$sql = "SELECT title,author FROM comics LIMIT 1';

$res = mysql_query($sql);

$row = mysql_fetch_assoc($res);

echo '<pre>'; print_r($row); echo '<pre>';

?>

Would print the following...

Array
(
    [title] => Elfquest
    [author] => Richard/Pini (Wendi)
)

Can be especially useful when using multidimensional arrays, or when you have alot of fields returned from the database.