Skip to Content
PHP: The Good Parts
book

PHP: The Good Parts

by Peter MacIntyre
April 2010
Beginner
174 pages
4h 24m
English
O'Reilly Media, Inc.
Content preview from PHP: The Good Parts

Chapter 7. Database Interaction

It would make little sense these days to have a static website that doesn’t change unless you physically alter the HTML or PHP of each file. There is usually a need to store and retrieve dynamic information as it relates to the content of a website or web application. In this chapter, we will look at how to make your pages draw some of their content from a database.

MySQLi Object Interface

The most popular database platform used with PHP is the MySQL database. If you look at the MySQL website you will discover that there are a few different versions of MySQL you can use. We will look at the freely distributable version known as the community server. PHP has a number of different interfaces to this database tool as well, so we will look at the object-oriented interface known as MySQL Improved extension (MySQLi). If you read the previous chapter on OOP with PHP, the use of this interface should not be overly foreign.

First, let’s use the very basic database schema I hinted at in the previous chapter by extending the example of a rudimentary guestbook page we started with. We’ll add the ability to actually save the entries into the database table. Here is the structure of the guests table:

table: guests
guestid  int(11)
fname      varchar(30)
lname      varchar(40)
comments text

And here is the SQL code to create it:

CREATE DATABASE 'website' ; USE 'website' ; CREATE TABLE 'guests' ( 'guestid' INT NOT NULL AUTO_INCREMENT PRIMARY KEY , 'fname' VARCHAR( 30 ) NOT NULL , ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

PHP Cookbook

PHP Cookbook

David Sklar, Adam Trachtenberg
PHP Cookbook

PHP Cookbook

Eric A. Mann
Better PHP Development

Better PHP Development

Younes Rafie, Reza Lavaryan, Christopher Pitt, Bruno Skvorc, Thomas Punt
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 9781449387761Errata Page