PHP 5/MySQL Programming- P68 docx

5 175 0
PHP 5/MySQL Programming- P68 docx

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

Thông tin tài liệu

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 SOURCE command to load that code. Here is an SQL script for creating the phoneList database: ## 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 C h a p t e r 9U s i n g M y S Q L t o C r e a t e D a t a b a s e s FIGURE 9.8 The result of the SELECT statement is a table, just like the original plan. 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 DROP command. The following command does exactly that: DROP TABLE IF EXISTS phoneList; If the phoneList table 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 .sql extension. Inside MySQL, you can use the SOURCE command to load and execute a script file. Figure 9.9 shows MySQL after I run the buildPhonelist.sql script. 314 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r 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. TRAP 315 C h a p t e r 9U s i n g M y S Q L t o C r e a t e D a t a b a s e s FIGURE 9.9 The SOURCE command allows you to read in SQL instructions from a file. IN THE REAL WORLD 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. 316 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n 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 htdocs if 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 TRICK FIGURE 9.10 The main phpMyAdmin screen lets you choose a database in the left frame or perform administrative tasks in the main frame. 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. TRICK 317 C h a p t e r 9U s i n g M y S Q L t o C r e a t e D a t a b a s e s FIGURE 9.11 It’s easy to create a table and modify its structure with phpMyAdmin. . 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. 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. 316 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r phpMyAdmin. structures. The program is written in PHP, so install it to your server’s HTML document path (usually htdocs if you’re using the Apache server). Some of the more advanced phpMyAdmin features—including

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

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Chapter 1: Exploring the PHP Environment

    • Chapter 2: Using Variables and Input

    • Chapter 3: Controlling Your Code with Conditions and Functions

    • Chapter 4: Loops and Arrays

    • Chapter 5: Better Arrays and String Handling

    • Chapter 6: Working with Files

    • Chapter 7: Writing Programs with Objects

    • Chapter 8: XML and Content Management Systems

    • Chapter 9: Using MySQL to Create Databases

    • Chapter 10: Connecting to Databases within PHP

    • Chapter 12: Building a Three-Tiered Data Application

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan