.... Internet marketing resources, ecommerce web site design tutorials
  Taming the Beast - quality web marketing and ecommerce development services .... .

One of the worlds most popular PHP/MySQL tutorial manuals - fully revised edition!
Learning PHP doesn't have to be difficult if you have a great manual - read a free chapter from 
"Build Your Own Database Driven Website Using PHP & MySQL". This manual is full of great PHP 
and MySQL development tutorials that will have you creating your first web project quickly & easily.

 

Click here to return to Taming the Beast's Home Page

MySQL & PHP Manual - Build your own database driven web site!

Finally, a practical, down-to-earth guide to learning PHP & MySQL!

We hope you enjoy this sample chapter from "Build Your Own Database Driven Website Using PHP & MySQL" This book is only available from the SitePoint Website for US$34.95. It also comes with electronic access to all PHP and MySQL code samples used throughout the book. 

PHP-MySQL book - download free chapters

Build Your Own Database Driven Website Using PHP & MySQL" 4 Chapters (over 100 pages) that you can download for free!

PHP developer manual - free sample download ‘ Practical Solutions to common problems ’  PHP Anthology is a complete guide for any PHP developer Download free manual chapters

Page - 1 - 2 - 3 - 4 - 5 - 6

You are on Page 1

Chapter 4 - Publishing MySQL Data on the Web

This is it -- the stuff you signed up for! In this chapter, you'll learn how to take information stored in a database and display it on a Web page for all to see. So far you have installed and learned the basics of MySQL, a relational database engine, and PHP, a server-side scripting language. Now you'll see how to use these two new tools together to create a true database-driven Web site!

A Look Back at First Principles

Before we leap forward, it's worth a brief look back to remind ourselves of the goal we're working toward. We have two powerful, new tools at our disposal: the PHP scripting language, and the MySQL database engine. It's important to understand how these two will fit together.

The whole idea of a database-driven Web site is to allow the content of the site to reside in a database, and for that content to be dynamically pulled from the database to create Web pages for people to view with a regular Web browser. So on one end of the system you have a visitor to your site who uses a Web browser to load http://www.yoursite.com, and expects to view a standard HTML Web page. On the other end you have the content of your site, which sits in one or more tables in a MySQL database that only understands how to respond to SQL queries (commands).

/# image 'phpmysql-ch4-fig1 | center' #/

As shown in the diagram above, the PHP scripting language is the go-between that speaks both languages. It processes the page request and fetches the data from the MySQL database, then spits it out dynamically as the nicely-formatted HTML page that the browser expects. With PHP, you can write the presentation aspects of your site (the fancy graphics and page layouts) as "templates" in regular HTML. Where the content belongs in those templates, you use some PHP code to connect to the MySQL database and -- using SQL queries just like those you used to create a table of jokes in Chapter 2 -- retrieve and display some content in its place.

Just so it's clear and fresh in your mind, this is what will happen when someone visits a page on our database-driven Web site:

  • The visitor's Web browser requests the Web page using a standard URL

  • The Web server software (Apache, IIS, or whatever) recognizes that the requested file is a PHP script, and so the server interprets the file using its PHP plug-in, before responding to the page request.

  • Certain PHP commands (which we have yet to learn) connect to the MySQL database and request the content that belongs in the Web page. 

  • The MySQL database responds by sending the requested content to the PHP script. 

  • The PHP script stores the content into one or more PHP variables, and then uses the now-familiar echo function to output the content as part of the Web page.

  • The PHP plug-in finishes up by handing a copy of the HTML it has created to the Web server.

  • The Web server sends the HTML to the Web browser as it would a plain HTML file, except that instead of coming directly from an HTML file, the page is the output provided by the PHP plug-in.

Connecting to MySQL with PHP

Before you can get content out of your MySQL database for inclusion in a Web page, you must first know how to establish a connection to MySQL from inside a PHP script. Back in Chapter 2, you used a program called MySQL that allowed you to make such a connection. PHP has no need of any special program, however; support for connecting to MySQL is built right into the language. The following PHP function call establishes the connection:

mysql_connect(address, username, password);

Here, address is the IP address or hostname of the computer on which the MySQL server software is running ("localhost" if it's running on the same computer as the Web server software), and username and password are the same MySQL user name and password you used to connect to the MySQL server in Chapter 2.

You may or may not remember that functions in PHP usually return (output) a value when they are called. Don't worry if this doesn't ring any bells for you -- it's a detail that I glossed over when I first discussed functions. In addition to doing something useful when they are called, most functions output a value, and that value may be stored in a variable for later use. The mysql_connect function shown above, for example, returns a number that identifies the connection that has been established. Since we intend to make use of the connection, we should hold onto this value. Here's an example of how we might connect to our MySQL server. (continued on next page)

Read on....

Page - 1 - 2 - 3 - 4 - 5 - 6

 

PHP-MySQL book - download free chapters

Build Your Own Database Driven Website Using PHP & MySQL" 4 Chapters (over 100 pages) that you can download for free!

PHP developer manual - free sample download ‘ Practical Solutions to common problems ’  PHP Anthology is a complete guide for any PHP developer Download free manual chapters

 

Home

 

Get paid cash taking online surveys - free to join online 
survey companies that will pay you cash for your opinion!

In Loving Memory - Mignon Ann Bloch

copyright (c) 1999-2011  Taming the Beast  Adelaide - South Australia 

Profile - Contact - Privacy - Consultants Portfolio 

Search Site - Terms of Service - Social/environmental