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

Secure PHP Development- P158 pot

5 60 0

Đ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 124,65 KB

Nội dung

Now suppose we want to create another application that deals with PocketPCs. Because PocketPCs are really PDAs with specific operating system (Windows CE) requirements, we can easily inherit the PDA class and define a new class called PocketPC as follows: <?php require_once ‘class.PDA.php’; class PocketPC extends PDA { function printSpec($type = null) { echo “CPU : “ . $this->getCPU() . “<br>”; echo “RAM : “ . $this->getRAM() . “<br>”; echo “TYPE : “ . $this->getType() . “<br>”; echo “Windows CE Only System <br>”; } } ?> In the preceding example, we have extended PDA and overridden the printSpec() method, as we want to print the fact that PocketPCs only run the Windows CE operating system. Therefore, an application such as the following can use this class: <?php require_once ‘class.PocketPC.php’; $info[‘CPU’] = ‘StrongArm 400 Mhz’; $info[‘RAM’] = ‘512 GB’; $info[‘TYPE’] = ‘PDA’; $myGizmo = new PocketPC($info); $myGizmo->printSpec(); ?> Appendix B: PHP Primer 761 32 549669 AppB.qxd 4/4/03 9:28 AM Page 761 This will output the following: CPU : StrongArm 400 Mhz RAM : 512 GB TYPE : PDA Windows CE Only System The classes discussed so far clearly show the power of OOP. Notice how we over- ride the PDA’s printSpec() method in the PocketPC class, but retain all the other benefits of the PDA, which happens to be a Computer object. 762 Part VII: Appendixes 32 549669 AppB.qxd 4/4/03 9:28 AM Page 762 Appendix C MySQL Primer MYSQL IS THE MOST POPULAR open-source database in the world. Its popularity stems from the following: ◆ MySQL is free as long as you don’t sell it to someone, sell a product that is bundled with it, or install and maintain it at a client site. If you’re in doubt about whether you fit within the license parameters, please see the Web site at http://www.mysql.com/. ◆ MySQL supports many programming interfaces, including PHP, C, C++, Java, Perl, and Python. The possibilities it offers to tailor programs to fit your needs are virtually limitless. ◆ MySQL uses very fast methods of relating tables of information to one another. Using a method called a one-sweep multijoin, MySQL is very effi- cient at gathering the information you request from many different tables at once. ◆ MySQL is widely used. Chances are good that many other people have done something similar to what you are doing. If you have questions or problems, you have a wide group of people to consult. Not only can you get advice from others about what to do, you can also get valuable infor- mation about what not to do. This prevents you from making the same mistakes others have made. MySQL is available all over the Internet. The best way to get MySQL is to go to http://www.mysql.com/ and find a mirror site close to you. You can find out how to install MySQL on a Linux platform in Appendix D. 763 33 549669 AppC.qxd 4/4/03 9:28 AM Page 763 Using MySQL from the Command-Line You can start the MySQL client program by typing the following: mysql -u username -p In this example, username is the username you are using to access the SQL server. If a password is required, you are prompted for it. You should now see some- thing like the following: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 143 to server version: 3.23.52 Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer. mysql> Creating a database When you install MySQL, you have no data. In fact, you don’t even have a database defined, other than the ones provided by MySQL itself. In this section, we will cre- ate a database called store. The syntax for this, at the mysql> prompt is simply create database store; You should get a response similar to the following: Query OK, 1 row affected (0.02 sec) This generic response indicates that your command has executed. You can con- firm this by issuing the following command: show databases; Your rights to create, change, or delete databases depends on your account and the rights associated with it. If you have root access you can (of course) do just about anything, including set parameters for other accounts. This chapter assumes that you have enough access to create and change data- bases. If you do have root access to MySQL, be sure to set/change the root password after installing MySQL! 764 Part VII: Appendixes 33 549669 AppC.qxd 4/4/03 9:28 AM Page 764 If this is the first database you create, you see the following: + + | Database | + + | mysql | | test | | store | + + 1 row in set (0.00 sec) Make sure you terminate each SQL query with a semicolon. Without a semi- colon terminator, you will see another prompt line and your query will not be executed. There it is. You now have a database named store that contains no data. Now it is up to you to create the tables that store the data. You need to define not only the names of all the columns, but also the types of data they store. Begin by identify- ing the database you’re going to use by issuing the following command: Use store; Then issue the following command to make the first table: create table customers ( id INT AUTO_INCREMENT PRIMARY KEY, name CHAR(40) NOT NULL, address CHAR(80), telephone CHAR(13)); Let’s examine each of these lines to see what it does, beginning with the first: id INT AUTO_INCREMENT PRIMARY KEY, This line is the meat of the table. In it, you are creating a column named id that holds whole numbers (int stands for integer). Additionally, this column is the pri- mary key. A primary key provides a convenient element to access the data in an orderly fashion. For example, in our database the records will be stored with unique integers as their primary key. Later, to access a record you could use a query to specify what record(s) to return by using that key. Searching for matches with a pri- mary key is significantly faster than other fields. Appendix C: MySQL Primer 765 33 549669 AppC.qxd 4/4/03 9:28 AM Page 765 . easily inherit the PDA class and define a new class called PocketPC as follows: < ?php require_once ‘class.PDA .php ; class PocketPC extends PDA { function printSpec($type = null) { echo “CPU :. system. Therefore, an application such as the following can use this class: < ?php require_once ‘class.PocketPC .php ; $info[‘CPU’] = ‘StrongArm 400 Mhz’; $info[‘RAM’] = ‘512 GB’; $info[‘TYPE’]. GB’; $info[‘TYPE’] = ‘PDA’; $myGizmo = new PocketPC($info); $myGizmo->printSpec(); ?> Appendix B: PHP Primer 761 32 549669 AppB.qxd 4/4/03 9:28 AM Page 761 This will output the following: CPU :

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