Advanced formatting
Perhaps the most popular way to lay out database results is using HTML tables where each row in the database is a row in the table, and each column in the database is a column in the table. In order to give you a head start in this common task, here's an example of table printing in action. The SQL schema used is this:
CREATE TABLE dogbreeds (ID INT AUTO_INCREMENT PRIMARY KEY, Name CHAR(50), Size CHAR(10), Info CHAR(50));
Go ahead and add your own fields to that to fill it up, then create a script with the following code:
<?php
mysql_connect ( "localhost" , "phpuser" , "alm65z" );
mysql_select_db ( "phpdb" );
$result = mysql_query ( "SELECT Name, Size, Info FROM dogbreeds ORDER BY Name;" );
if ( mysql_num_rows ( $result )) {
print "<table=\"1\"><tr style=\"font-weight: bold;\"><td>Breed</td><td>Size</td><td>Info</td></tr>" ;
while( $row = mysql_fetch_assoc ( $result )) {
extract ( $row );
print "<tr><td>$Name</td><td>$Size</td><td>$Info</td></tr>" ;
}
print "</table>" ;
} ?>
As you can see, it is the same "iterating through mysql_fetch_assoc() " approach we've used previously, but now we're using it to create an HTML table row by row - as you can see from the screenshot below, it works very well.
Next chapter: Reading auto-incrementing values >>
Previous chapter: Results within results
Jump to: Databases Introduction Database hierarchy Types of data Date and time Transactions Stored procedures Triggers Views Keys Referential integrity Indexes Persistent connections Temporary Tables Table handlers Round up History MySQL PostgreSQL Oracle Microsoft SQL Server SQL SQL comments Interacting with MySQL Creating tables Making table changes Deleting tables Inserting data Selecting data Extra SELECT keywords Updating data Deleting data MySQL for dummies A working example Multiple WHERE conditions Grouping rows together with GROUP BY MySQL functions Managing indexes Simple text searching using LIKE Advanced text searching using full-text indexes Range matching Working with NULL Default values Using MySQL with PHP Connecting to a MySQL database Querying and formatting Disconnecting from a MySQL database Reading in data Mixing in PHP variables Results within results Advanced formatting Reading auto-incrementing values Unbuffered queries for large data sets phpMyAdmin PEAR::DB Quick PEAR::DB calls Query information Advanced PEAR::DB Impeared performance? SQLite Using SQLite Before you begin Getting started Advanced functions Mixing SQLite and PHP Normalisation Why separate data? So, what is the solution here? Why not separate data? First normal form Second normal form Other normal forms Conclusion Table joins Complex joins Using temporary tables Adjusting the priority queue How to design your tables Picking the perfect data type When MySQL knows best Persistent connections Choosing a table type Transactions MySQL Improved Subselects, views, and other advanced functions Subselects Views Referential integrity Summary Exercises Further reading Next chapter
Home: Table of Contents
Follow us on Identi.ca or Twitter