cs 360 information on easyphp based on information in php, mysql, and javascript, by robin nixon...

90
CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

Upload: kathryn-hicks

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

1

CS 360Information on EasyPHP

Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon

Prepared by Kirk Scott

Page 2: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

2

Introduction to EasyPHP and MySQL

• This version of the document contains a summary of the instructions given in the book, as well as solutions to roadblocks.

• This document is a simplified replacement for the content in the Nixon book.

• Assuming that you have a question or a problem, before concluding that there is no answer or solution to it, do a quick scan of all of the points covered in this document.

Page 3: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

3

• The most important thing to keep in mind is this:

• This document is provided as a survey of where the things came from that became part of the project

• It is not intended to be a complete replacement for a resource on Web development of a MySQL database

Page 4: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

4

• The Web is the most up to date resource on how to accomplish the kinds of things required

• Check out this Web site:• http://www.w3schools.com/• The existence of sites like these have rendered

lots of textbooks more or less obsolete

Page 5: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

5

Part I. Downloading and Installing EasyPHP with MySQL, etc. Topics in Chapter 2 of the Book.

Page 6: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

6

A. Installing EasyPHP as a local Windows, Apache, MySQL, and PHP development server.

• 1. Go to this address:• www.easyphp.org• A screenshot is given on the next overhead

Page 7: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

7

Page 8: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

8

• 2. Click the download button near the upper right hand corner of the screen.

• 3. Follow the instructions for downloading; then follow the instructions for installing.

Page 9: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

9

B. Running and Using EasyPHP.

• 1. At the end of a successful installation you will automatically be taken to the EasyPHP Help page.

• As usual, the installation may be fraught with little difficulties.

• It’s possible to actually install successfully, but have it not go to the help page.

Page 10: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

10

• If that happens, just type in the URL yourself.• http://127.0.0.1/home/en/index.html• Different versions may have slightly different

addresses• This matter of different addresses will recur in

these overheads• A screenshot of what you might see is given

on the next overhead.

Page 11: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

11

Page 12: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

12

• 2. The help page provides this initial information:

• 3. A successful installation should put an EasyPHP icon in the system tray at the bottom of the screen.

• 4. Double clicking on the icon allows you to check whether Apache and MySQL are running, and restart them if they're not.

Page 13: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

13

• 5. Right clicking on the icon gives a menu including these options: Administration, Local Web, Restart, and Stop.

• 6. Administration allows you to display your database, once it's created.

• 7. Local Web allows you to display your files, once you have files to display.

Page 14: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

14

• 8. Restart and Stop allow you to start and stop EasyPHP in its entirety, rather than dealing with Apache and MySQL individually.

• 9. Over time, you may discover that EasyPHP stops on its own for unknown reasons.

• This is the way to start the whole thing running again.

Page 15: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

15

C. Testing EasyPHP.

• 1. Right click on the EasyPHP icon in the system tray and take the Local Web option in the menu.

• A screenshot is shown on the next overhead

Page 16: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

16

Page 17: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

17

• 2. The Web browser will open to a URL of this form: http://127.0.0.1:8888/.

• 3. This is the address for the local host with port 8888 instead of port 80, the usual default for a Web host.

• Depending on the version you have installed, the address may have 8887 or some other value.

• EasyPHP does this in order to avoid conflicts with other programs that use the network.

Page 18: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

18

• 4. What you are seeing on this page is a directory listing for the home directory for the server, which should initially be empty.

• 5. Now right click on the EasyPHP icon in the system tray and take the Administration option in the menu.

• A screenshot is shown on the following overhead

Page 19: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

19

Page 20: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

20

• 6. A Local Web will be listed, showing a path name on the computer.

• 7. If you click on the Root option, this will take you to the directory you just accessed through the previous steps.

• 8. If you want to, you can take the Alias option and set up a directory different from the default directory to work in.

Page 21: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

21

D. Possible Problems.

• 1. Attempting to run EasyPHP may generate Apache or MySQL errors.

• 2. These errors may indicate that an attempt has been made to access a blocked port.

• 3. Firewalls, antivirus and other security programs, Skype, etc. may block ports.

Page 22: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

22

• 4. In general, port 80 (or its equivalent) has to be available for Apache and port 3306 (or its equivalent) has to be available for MySQL.

• EasyPHP defaults to port 8888 or 8887 or some other port, but conflicts with this port would still be possible.

• 5. No attempt will be made here to summarize how to debug and fix problems with port access.

Page 23: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

23

• 6. I looked into it a little and discovered that a Web search on the topic brought up useful information at the Web site superuser.com, for example.

• 7. Needless to say, if you have problems like these, trying to fix them could take you down a long and winding path.

Page 24: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

24

• 8. Another possible source of problems is that depending on the version of Windows you're using, the installation directory of EasyPHP might not end up with the right default security permissions.

• 9. The directory where EasyPHP is installed, and its subdirectories, should have write access for all users.

• 10. Go to the EasyPHP directory in the Program Files directory, right click on it, take the Properties option in the menu, and click on the Security tab to check on this.

Page 25: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

25

Part II. Working with MySQL. Topics in Chapter 8 in the Book.

Page 26: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

26

A. There is a graphical user interface for MySQL on EasyPHP.

• 1. The GUI is accessed through the phpMyAdmin page, which can be found at this address: http://127.0.0.1:8888/home/mysql/.

• 2. You can certainly use the GUI if you want to• If you do, you can figure it out yourself.• 3. Everything that you need to do can be

accomplished through the command line interface, and it will be covered below.

Page 27: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

27

B. Accessing the MySQL Command Line Interface.

• 1. You can get to a command prompt in Windows by clicking the Start button and either entering cmd into the text field at the bottom or going to the list of all programs and looking in the Accessories folder.

• 2. At the command prompt, using cd and dir, you want to navigate to this folder, where x.x.x.x stands for whatever version of EasyPHP you installed:

• C:\Program Files\EasyPHP-x.x.x.x\mysql\bin.

Page 28: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

28

• 3. Enter this command at the prompt in order to log in as the root: mysql –u root –p.

• 4. If successful, the command will prompt you for a password.

• 5. As the root, you don't need a password, so you complete the login by hitting the enter key.

Page 29: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

29

• 6. You will be greeted by some jargon about the program, followed by this statement:

• Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

• 7. This will be followed by the mysql command prompt: mysql>

Page 30: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

30

C. Using the MySQL Command Prompt.

• 1. MySQL contains database management commands in addition to vanilla SQL DDL and DML commands.

• 2. Suppose you enter this command: • SHOW databases• You should be shown a list of all databases

currently in existence and being managed by the installation of MySQL you're working with.

Page 31: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

31

• 3. If everything has gone OK with the installation so far this is what you should see as a result:

• • +--------------------+• | Database |• +--------------------+• | information_schema |• | mysql |• | phpmyadmin |• +--------------------+

Page 32: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

32

• 4. If you type in a complete SQL command, with a semicolon at the end, and it's in error, you will get an audible warning that something is amiss.

• 5. You will quickly discover that MySQL commands have to be followed by a semicolon.

Page 33: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

33

• Typing in anything that doesn't end with a semicolon, whether a partial or complete command, will take you to a sub-prompt of this form: .

• This tells you that MySQL is waiting for the rest of a command or the completion of a command.

• If you already typed it all in except for the semicolon, you can just enter the semicolon at this prompt, followed by the enter key, and the command will be executed.

Page 34: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

34

D. Creating a New Database.

• 1. It is simple to create a new database. Enter a command of this form:

• CREATE DATABASE DatabaseName;• 2. You can verify that it has been created by

repeating this command: • SHOW databases;

Page 35: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

35

E. Creating Users and Granting Access.

• 1. In the long run you would probably want to have a regular login rather than using the Root login.

• 2. The Root has the ability to create other users. The command is:

• CREATE user Userid;

Page 36: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

36

• 3. It is possible to set a password for the user, but for the time being doing that isn't critical.

• 4. Once a database has been created and a user has been created, the user can be granted access to the database with this command:

• GRANT ALL ON DatabaseName.* TO 'Userid';

Page 37: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

37

F. Using Databases and Exiting MySQL.

• 1. If you enter the 'help' command, you will be shown a list of all MySQL (system) commands.

• The commands in this list that you can't live without are:

• USE, EXIT, QUIT.

Page 38: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

38

• 2. If a user has been granted access to a database and wants to use the database, it is necessary to enter this command:

• USE DatabaseName;• 3. When a user is finished and wants to leave

the MySQL command prompt, it's necessary to enter either of these two commands:

• EXIT; QUIT;

Page 39: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

39

G. Creating Tables and inserting Data in MySQL.

• 1. At this point you can apply your knowledge of SQL to create tables with CREATE TABLE commands.

• 2. Since table creation was illustrated with Microsoft Access, it's worthwhile to note a few of the difference between the implementation of SQL in that product and SQL in MySQL.

Page 40: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

40

• 3. The TEXT type exists in MySQL, but it is not the preferred type. CHAR or VARCHAR are preferred.

• There are subtle differences between VARCHAR and TEXT, the most important being that if you choose to index on a TEXT field, you have to specify how many characters to index on.

Page 41: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

41

• 4. The CURRENCY data type doesn't exist in MySQL. • The preferred type for declaring monetary fields is

DECIMAL. • A declaration of a DECIMAL field takes this form: • DECIMAL(m, n). • m tells the total number of digits the number can

contain. • n tells how many of those digits follow the decimal

point.

Page 42: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

42

• 5. MySQL supports an AUTO_INCREMENT type, which may be useful for primary key fields.

• 6. There is another useful command in MySQL:

• DESCRIBE TableName. • This gives you a summary of the design of the

table, separate from the SQL table creation statement.

Page 43: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

43

• 7. Keep in mind that part of the requirements for the project is documenting table designs.

• Some combination of SQL CREATE TABLE commands and output from the DESCRIBE command should provide this information.

Page 44: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

44

• 8. If you're working at the command prompt, it will probably be convenient to create longer statements in an editor and then paste them into the command prompt.

• If you're working with the GUI, you will have to explore to find out how you might generate the information you'll need for documentation.

Page 45: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

45

• 9. After table creation is complete, initial data insertion is the next task.

• If you're working at the command prompt, SQL INSERT statements can be used.

• Although this may seem inconvenient, it isn't really.

Page 46: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

46

• You can easily prepare long sequences of similar insertion statements in an editor and then paste them into the command prompt.

• This has the advantage that if something goes wrong, you can always repopulate the tables with the saved files containing the insertion statements.

• If you're working with the GUI, you'll have to explore to find out the most effective way to insert data.

Page 47: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

47

• 10. When writing insertion statements (and later on, when writing queries) keep in mind that MySQL doesn't recognize # signs for enclosing date values.

• Date field values should be enclosed in single quotes, just like CHAR or VARCHAR values.

Page 48: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

48

H. Indexing in MySQL.

• 1. In the presentation of indexing earlier in the semester, creating an index was handled separately from creating the table which had the index.

• This can be done with MySQL.• 2. It was also pointed out that under the covers,

primary key field characteristics are typically enforced by default indexes on those fields.

• This also holds true in MySQL.

Page 49: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

49

• 3. MySQL also supports syntax for specifying an index as part of the table definition.

• 4. Indexing in MySQL involves certain complexities.

• For example, it supports different kinds of indexes.

• One is specified by including this keyword phrase after the declaration of the index:

• ENGINE MyISAM.

Page 50: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

50

• 5. The details of these complexities are of no consequence.

• After unit 19 you will be in a position to appreciate at least in part what's going on.

• ISAM stands for indexed sequential access method.

• What you can infer is that supporting an index may involve the organization and storage of the file itself.

Page 51: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

51

• 6. In MySQL with CHAR fields, you have the option of indexing on a subset of the field.

• Most likely you will want to index on the full field. • If you are indexing on VARCHAR fields, most likely you

will want to index on the minimum number of characters necessary to distinguish different entries in the field.

• There is a concept of FULLTEXT indexes, but all that matters is that you create some functional indexes, not that you master the details.

Page 52: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

52

• Just like with key fields, documentation might be relatively easy to do if have done this with a CREATE INDEX statement which you've saved.

• If you've done this somehow through the graphical user interface, you'll have to explore to find out how to document what you did.

Page 53: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

53

• 7. The same general rule applies here that applies everywhere in database design and implementation.

• If you have a correct design from the start and don't have to make changes after you've already started creating tables, populating them, and creating indexes, life will go smoothly.

• If changes are needed, things can get messy, especially involving primary key fields and indexing.

Page 54: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

54

I. Concluding Blah Blah Blah on Part II. Working with MySQL

• 1. Some of the SQL syntax in MySQL differs somewhat from the syntax of Microsoft Access, which was presented earlier in the semester.

• 2. As you have already seen, MySQL also has commands like SHOW and DESCRIBE which are not SQL, but can be useful in managing MySQL databases.

• 3. This presentation is obviously limited to generalities and high points.

• You will need to use books or online documentation to get details and specific syntax for particular tasks.

Page 55: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

55

• 4. As noted earlier, you may choose to use the GUI, which can be accessed at this address: http://127.0.0.1:8888/home/mysql/.

• 5. The GUI is convenient, but it would take some effort to master it since it has a lot of stuff in it.

• 6. Although the GUI should be user friendly, it is not unusual for some things to be easier or more straightforward though a command line interface.

Page 56: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

56

• 7. This presentation covers only the command line interface because that is the minimum you would need to know in order to do the project.

• 8. You will have to make your own decision about whether the ease of use is worth the cost of learning the GUI.

Page 57: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

57

• You should also keep in mind the need to generate documentation.

• Some of it can be generated through the command line interface.

• Whether it can be comprehensively shown with output from the GUI is not known.

Page 58: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

58

• 9. Even if you do use the GUI when developing your project, you may still want to use the command line interface for generating at least some of the documentation.

• Also, it's not a bad idea to save some of your work, like table creation and insertion commands, as text files for future reference, whether when recreating a database or when documenting it.

Page 59: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

59

Part III. Using EasyPHP to Write .php Script Files That Run MySQL Web Queries. Topics in Chapter 10 in the

Book.

Page 60: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

60

A. General Ideas.

• 1. Note that I am effectively not covering PHP at all.

• It is a complete language with its own syntax, etc.

• The parts of PHP you need in order to do the project are pretty self-explanatory to anyone who has learned any other language.

• I will just show some examples, basically.

Page 61: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

61

• 2. What will be covered in Part III are the rudiments of embedding SQL queries in PHP script files.

• The PHP script files obtain the query results and format and display them as output in a Web page.

• The first few lettered items cover part 9 of the project (Nixon, ch. 10, pages 225-231).

• Parts 10-12 are covered after that.

Page 62: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

62

• 3. If you've been following along with this presentation, the whole idea is that you will save your PHP scripts as php files in the local host directory.

• 4. All you have to do to run the scripts is open a .php script file in your browser.

Page 63: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

63

B. The Structure of a PHP Program for Database Use.

• 1. Connect to MySQL.• 2. Select a database to use.• 3. Build a query string.• 4. Execute the command that obtains a

reference into the results of the query.• 5. Acquire the actual query results and format

and display them.

Page 64: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

64

• 6. Note that PHP is procedural. • The typical query may retrieve more than one

row from a table. • This is handled by looping in PHP.• 7. When finished, disconnect from the MySQL

database.

Page 65: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

65

C. Logging in, Connecting, and Selecting a Database to Use.

• 1. This is an example of a login script in PHP.• • <?php // login script saved as login.php• $db_hostname = 'http://127.0.0.1:8888';• //hardcode the values on the right in the script.• $db_database = 'DatabaseName';• $db_username = 'UserName';• $db_password = 'PassWord';• ?>

Page 66: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

66

• 2. Hopefully the pieces of the script are clear. • Such a script can be included at the top of

programs so that you don't have to type it all in every time.

Page 67: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

67

• 3. This is an example of a PHP script that uses the login script and then connects to the database specified in the login script.

• • <?php• require_once 'login.php';• $db_server = mysql_connect($db_hostname, $db_username, $db_password);• if(!$db_server) die("Couldn't connect to MySQL: " . mysql_error());• ?>

Page 68: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

68

• 4. This short script illustrates selecting a database. • This sequence of PHP code snippets could

eventually be assembled into one script that did something useful.

• It's easier to present the topics piece by piece rather than in one program.

• • <?php• mysql_select_db($db_database) or die("Couldn't select

database: " . mysql_error());• ?>

Page 69: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

69

D. Creating a Query = Accessing the Database.

• 1. In a given script, once you have access to a database, it's possible to start thinking about querying it.

• The following snippet shows how to set up an SQL query in PHP.

• • <?php• $query = "SELECT * FROM TableName";• $result = mysql_query($query);• if(!$result) die("Couldn't query database: " . mysql_error());

• ?>

Page 70: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

70

• 2. Notice that there is no semicolon at the end of the query string.

• There should not be one.• 3. Notice that $result is not a table of results. • It is a resource, a handle for accessing the

results. • How to use the handle will be covered next.

Page 71: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

71

E. Fetching Results.

• 1. In a complete PHP script for database access, sequentially you would include logging in, connecting to MySQL, selecting a database, constructing a query, and obtaining the variable $result.

• 2. Following those steps, accessing the contents of the results would be accomplished with code like the snippet shown on the following overhead.

Page 72: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

72

• /* $result is the handle into the query results. */• /* $rows is the count of the number of rows in the results. */• /* $row is one row of the results. */• /* $row[0], $row[1], … are the field values in a row of results. */• /* The example snippet shows echoing the first (0th) field. */• /* You could echo all of the other fields the same way. */

• <?php• $rows = mysql_num_rows($result);• for($j = 1; $j <= $rows; $j++)• {• $row = mysql_fetch_row($result);• echo 'Put the field name here, for example' . $row[0] . '<br />';• }• ?>

Page 73: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

73

F. Closing a Connection.

• 1. Good housekeeping requires closing a connection when you're done with MySQL.

• The following snippet accomplishes this.• • <?php• mysql_close($db_server);• ?>

Page 74: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

74

G. Running a Script.

• 1. As stated above, when you have a complete script, all you have to do is store it as a .php file in the local host directory, then access it through a browser.

• If it's correct, the PHP code will be executed and the output of the script will be shown in a Web page.

Page 75: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

75

• 2. What I've overlooked telling you up to this point is that when you're working locally on your machine, this is the name of the directory where you want to store things:

• C:\Program Files\EasyPHP-x.x.x.x\www• As usual, the x.x.x.x will differ according to

which version of EasyPHP you have installed

Page 76: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

76

• 3. Rather than opening a browser window separately and typing in the Web address, when doing development it should be possible to test your scripts from EasyPHP.

• Right click on the EasyPHP icon in the system tray and take the Local Web option in the menu.

• A browser page will open to this address: http://127.0.0.1:8888/.

Page 77: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

77

• 4. This is the address of the www directory. • Your saved PHP files should appear there. • Clicking on a PHP file should cause it to run in

the browser.

Page 78: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

78

H. Adding and Deleting Records

• The example on the following overheads is taken from Nixon

• It shows the minimalist kind of form which would satisfy part of the project requirements

Page 79: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

79

Page 80: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

80

• <?php // sqltest.php• require_once 'login.php';• $db_server = mysql_connect($db_hostname, $db_username, $db_password);

• if(!$db_server) die("Unable to connect to MySQL: " . mysql_error());

• mysql_select_db($db_database, $db_server)• or die("Unable to select database: " . mysqlerror());

• if(isset($_POST['author']) &&• isset($_POST['title']) &&• isset($_POST['category']) &&• isset($_POST['year']) &&• isset($_POST['isbn']))• {• $author = get_post('author');• $title = get_post('title');• $category = get_post('category');• $year = get_post('year');• $isbn = get_post('isbn');

Page 81: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

81

• if(isset($_POST['delete']) && $isbn != "")• {• $query = "DELETE FROM classics WHERE isbn = '$isbn'";

• if(!mysql_query($query, $db_server))• echo "DELETE failed: $query<br />" .• mysql_error() . "<br /><br />";• }• else• {• $query = "INSERT INTO classics VALUES" .• "('$author', '$title', '$category', '$year', '$isbn')";

• if(!mysql_query($query, $db_server))• echo "INSERT failed: $query<br />" .• mysql_error() . "<br /><br />";• }• }

Page 82: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

82

• echo <<<_END• <form action = "sqltest.php" method = "post"><pre>• Author <input type = "text" name = "author" />• Title <input type = "text" name = "title" />• Category <input type = "text" name = "category" />• Year <input type = "text" name = "year" />• ISBN <input type = "text" name = "isbn" />• <input type = "submit" value = "ADD RECORD" />• </pre></form>• _END;

Page 83: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

83

• $query = "SELECT * FROM classics";• $result = mysql_query($query);

• if(!$result) die("Database access failed: " . mysql_error());• $rows = mysql_num_rows($result);

• for($j = 0; $j < $rows; ++$j)• {• $row = mysql_fetch_row($result);• echo <<<_END• <pre>• Author $row[0]• Title $row[1]• Category $row[2]• Year $row[3]• ISBN $row[4]• </pre>

Page 84: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

84

• <form action = "sqltest.php" method = "post">• <input type = "hidden" name = "delete" value = "yes" />• <input type = "hidden" name = "isbn" value = "$row[4]" />• <input type = "submit" value = "DELETE RECORD" /></form>• _END;• }

• mysql_close($db_server);

• function get_post($var)• {• return mysql_real_escape_string($_POST[$var]);• }• ?>

Page 85: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

85

I. Additional Features Like Check Boxes, Radio Buttons, and Drop Down Lists

• The following three examples are taken from Nixon

• They show the minimal kinds of features that could be added to forms in order to earn the second 5 points on some parts of the project

Page 86: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

86

Page 87: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

87

Page 88: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

88

Page 89: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

89

J. When Things Go Wrong.

• 1. You can try to debug an installation when things go wrong.

• 2. You can try reinstalling on the same machine.• 3. I have to admit that on one occasion when I

was having problems I had to install on a different machine in order to get the environment to work.

• 4. Hopefully the later versions of EasyPHP are more stable and less prone to conflict with other programs for ports, etc.

Page 90: CS 360 Information on EasyPHP Based on Information in PHP, MySQL, and JavaScript, by Robin Nixon Prepared by Kirk Scott 1

90

The End