1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P68 docx

5 179 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 267,37 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Writing a Script to Build a TableIt is very important to understand how to create tables by hand in SQL, because your programs have to do this same work.. When you create real data appli

Trang 1

Writing a Script to Build a Table

It is very important to understand how to create tables by hand in SQL, because

your programs have to do this same work However, it’s very tedious to write your

SQL code in the MySQL window directly When you create real data applications,

you often have to build and rebuild your data tables several times before you are

satisfied with them, and this would be awkward in the command-line interface

Also, as you are writing programs that work with your database, you will likely

make mistakes that corrupt the original data

It’s good to have a script ready for easily rebuilding the database with test data

Most programmers create a script of SQL commands with a text editor (use the

same editor in which you write your PHP code) and use the SOURCEcommand to

load that code Here is an SQL script for creating the phoneListdatabase:

## build phone list

## for mySQL

USE chapter9;

DROP TABLE IF EXISTS phoneList;

CREATE TABLE phoneList (

id INT PRIMARY KEY,

firstName VARCHAR(15),

lastName VARCHAR (15),

313

i n

FIGURE 9.8

The result of the

SELECT statement

is a table, just like

the original plan.

Trang 2

email VARCHAR(20),

phone VARCHAR(15)

);

INSERT INTO phoneList

VALUES (

0, ‘Andy’, ‘Harris’, ‘aharris@cs.iupui.edu’, ‘123-4567’

);

SELECT * FROM phoneList;

This code isn’t exactly like what I used in the interactive session, because the new code shows a few more features that are especially handy when you create SQL code in a script

Creating Comments in SQL

SQL is actually a language Although it isn’t technically a programming lan-guage, it has many of the same features Like PHP and other languages, SQL sup-ports several types of comment characters The # sign is often used to signify a comment in SQL Comments are especially important when you save a group of SQL commands in a file for later reuse These comments can help you remember what type of database you were trying to build It’s critical to put basic comments

in your scripts

Dropping a Table

It may seem strange to talk about deleting a table from a database before you’ve built one, but often (as in this case) a database is created using a script Before you create a new table, you should check to see if it already exists If it does exist, delete it with the DROPcommand The following command does exactly that: DROP TABLE IF EXISTS phoneList;

If the phoneListtable currently exists, it is deleted to avoid confusion

Running a Script with SOURCE

You can create an SQL script with any text editor It is common to save SQL scripts with the sqlextension Inside MySQL, you can use the SOURCEcommand to load and execute a script file Figure 9.9 shows MySQL after I run the buildPhonelist.sql script

314

g r

s o

l u

g in

e r

Trang 3

In Windows I often drag a file from a directory view into a command-line program like MySQL Windows copies the entire filename over, but it includes double quo-tation marks, which causes problems for the MySQL interpreter If you drag a filename into MySQL, edit out the quotation marks.

Working with a Database

via phpMyAdmin

It’s critical to understand the SQL language, but sometimes you may want an

alternative way to build and view your databases The command line is

func-tional, but it can be tedious to use If you are running a Web server, you can use

an excellent front end called phpMyAdmin This freeware program makes it

much easier to create, modify, and manipulate databases

T R A P

315

i n

FIGURE 9.9

The SOURCE

command allows

you to read in SQL

instructions

from a file.

I N THE R EAL W ORLD

The phpMyAdmin interface is so cool that you’ll be tempted to use it all the

time That’s fine, but be sure you understand the underlying SQL code—your

PHP programs have to work with plain-text SQL commands It’s fine to use a

front-end tool while building and manipulating your data, but your users won’t

use this program Your application is the user’s interface to your database, so

you must be able to do all commands in plain text from within PHP I use

phpMyAdmin, but I also make sure I always look at the code it produces so I can

write it myself.

Trang 4

g r

s o

l u

g in

e r

phpMyAdmin basically adds the visual editing tools of a program like Microsoft Access to the MySQL environment It also adds some wonderful tools for adding records, viewing your data structure, exporting data to useful formats, and experimenting with data structures The program is written in PHP, so install it

to your server’s HTML document path (usually htdocsif you’re using the Apache server)

Some of the more advanced phpMyAdmin features—including the ability to automate relationships and create PDF diagrams of your data structures—require table installation and some other special configuration If your server administrator has not enabled these features, consult an excellent tutorial at http://www garvinhicking.de/tops/texte/mimetutorial.

Connecting to a Server

MySQL is a client/server application The MySQL server usually runs on the Web server where your PHP programs reside You can connect a MySQL client such as phpMyAdmin to any MySQL server Figure 9.10 shows a connection to the local MySQL connection

It’s important to recognize that you can connect to any data server you have per-mission to use This data server doesn’t need to be on the same physical machine you are using This is useful if you want to use phpMyAdmin to view data on a

T R I C K

FIGURE 9.10

The main

phpMyAdmin

screen lets you

choose a database

in the left frame or

perform

administrative tasks

in the main frame.

Trang 5

remote Web server you are maintaining, for example However, many remote

Web servers are not configured to accept this kind of access, so you should know

how to work with the plain MySQL console

The first time you run phpMyAdmin, it will probably ask for some login information.

This data is stored so you don’t have to remember it every time However, if you want to change your login or experiment with some other phpMyAdmin features, edit the config.inc.php file installed in the main phpMyAdmin folder

Creating and Modifying a Table

phpMyAdmin provides visual tools to help you create and modify your tables The

phone list is way too mundane for my tastes, so I’ll build a new table to illustrate

phpMyAdmin features This new table contains a number of randomly generated

super heroes Select a table from the left frame and use the Create New Table

sec-tion of the resulting page to build a new table Figure 9.11 shows the dialog box

used to create a table or alter its structure

With phpMyAdmin you can choose variable types from a drop-down list; many

field properties are available as checkboxes It’s critical that you choose a variable

type (and a field length in case of character fields) When you finish creating or

modifying the table, the proper SQL code is generated and executed for you

T R I C K

317

i n

FIGURE 9.11

It’s easy to create a

table and modify its

structure with

phpMyAdmin.

Ngày đăng: 07/07/2014, 02:20

TỪ KHÓA LIÊN QUAN