HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [244]
print “
while ($field = mysql_fetch_field($result)){
print “
} // end while
print “
while ($row = mysql_fetch_assoc($result)){
print “
foreach ($row as $name => $value){
print “
} // end foreach
$count++;
print “
} // end while loop
print “ \n”;
} // end printResults
There’s only one twist here: SQL is now a parameter. This function won’t create the $sql variable itself. Instead, it accepts $sql as a parameter. I took out the line that created $sql as a hard-coded query because now I want to implement the query created by processInput(). Otherwise, the code in this function is a direct copy of contactTable.php.
Book VI
Managing Data with MySQL
Well-defined data is the central element in most commercial Web sites.
Chapter 1: Getting Started with Data
In This Chapter
Understanding databases, tables, records, and fields
Introducing the relational data model
Introducing a three-tier model
Understanding MySQL data types
Getting started with MySQL and phpMyAdmin
Adding a password to your MySQL root account
Creating new MySQL users
Designing a simple table
Adding data to the table
Most programs and Web sites are really about data. Data drives the Internet, so you really need to understand how data works and how to manage it well if you want to build high-powered, modern Web sites.
The trend in Web development is to have a bunch of specialized languages that work together. XHTML describes page content, CSS manages visual layout, JavaScript adds client-side interactivity, and PHP adds server-side capabilities. You’re probably not surprised when I tell you that yet another language, SQL (Structured Query Language), specializes in working with data.
In this minibook, you discover how to manage data. Specifically, you find out how to create databases, add data, create queries to retrieve data, and create complex data models to solve real-world problems. In this chapter, I show you some tools that automate the process of creating a data structure and adding data to it. In later chapters in this minibook, I show how to control the process directly through SQL.
Examining the Basic Structure of Data
Data has been an important part of programming since computing began. Many languages have special features for working with data, but through the years, a few key ideas have evolved. A system called relational data modeling has become the primary method for data management, and a standard language for this model, called SQL (Structured Query Language), has been developed.
SQL has two major components:
♦ Data Definition Language (DDL) is a subset of SQL that helps you create and maintain databases. You use DDL to build your databases and add data to them.
♦ Data Query Language (DQL) is used to pull data out of a database after it’s been placed there. Generally, your user input is converted to queries to get information from an existing database.
The easiest way to understand data is to simply look at some. The following table contains some basic contact information:
Name
Company
Bill Gates
Microsoft
bill@msBob.com
Steve Jobs
Apple
steve@rememberNewton.com
Linus Torvalds
Linux Foundation
linus@gnuWho.org
Andy Harris
Wiley Press
andy@aharrisBooks.net
Note: All these e-mail addresses are completely made up (except mine). Bill Gates hasn’t given me his actual e-mail address. He doesn’t answer my calls, either. . . . (sniff).
It’s very common to think of data in the form of tables. In fact, the fancy official database programmer name for this structure is table. A table (in database terms) is just a two-dimensional representation of data. Of course, some fancy computer-science words describe what’s in a table:
♦ Each row is a record. A record describes a discrete entity. In this table, each record is a person in an e-mail directory.
♦ A record is made of fields. All the records in this table have