Testing Your Installation


Now that you've installed everything and created the necessary MySQL users, you should test the installation. I'll create two quick PHP scripts for this purpose. In all likelihood, if an error occurred during one of the processes, you would already know it by now, but these steps will allow you to perform tests on your (or any other server) before getting into complicated PHP programming.

The first script being run is phpinfo.php. It both tests if PHP is enabled and shows a ton of information about the PHP installation. As simple as this script is, it is one of the most important scripts PHP developers ever write because it provides so much valuable knowledge.

The second script will serve two purposes. It will first see if support for MySQL has been enabled. If not, you'll need to see the next section of this chapter to change that. The script will also test if the MySQL user has permission to connect to a specific MySQL database.

To test PHP

1.

Create the following PHP document in a text editor (Script A.1).

 <?php phpinfo(); ?> 

Script A.1. The phpinfo.php script tests and reports upon the PHP installation.


The phpinfo() function returns the configuration information for a PHP installation in a table. It's the perfect tool to test that PHP is working properly.

You can use almost any application to create your PHP script as long as it can save the file in a plain text format.

2.

Save the file as phpinfo.php.

You need to be certain that the file's extension is just .php. Be careful when using Notepad on Windows, as it will secretly appended .txt. Similarly, TextEdit on Mac OS X wants to save everything as .rtf.

3.

Place the file in the proper directory on your server.

What the proper directory is depends upon your operating system and your Web server. If you are using a hosted site, check with the hosting company (although it should be fairly obvious once you FTP into the site). For Windows users who installed Apache, the directory is called htdocs and is within the Apache directory (C:\Program Files\Apache Group\Apache by default). For Mac OS X users, the proper directory is called Sites, found within your Home folder.

4.

Test in your Web browser (Figure A.23).

Figure A.23. The information for this server's PHP configuration.


Run this script in your Web browser by going to http://your.url.here/phpinfo.php. On your own computer, this may be something like http://localhost/phpinfo.php or http://localhost/~username/phpinfo.php, where ~username is your short username (Mac OS X).

To test PHP and MySQL

1.

Create a new PHP document in your text editor (Script A.2).

 <?php echo mysql_connect ('localhost',  'username, 'password'); ?> 

Script A.2. The mysql_test.php script tests for MySQL support in PHP and if the proper MySQL user privileges have been set.


This script will attempt to connect to the MySQL server using the username and password just established in this appendix. The results of this connection will be printed to the browser using the echo() statement.

2.

Save the file as mysql_test.php, upload to your server, and test in your Web browser (Figure A.24).

Figure A.24. The PHP script was able to connect to the MySQL server.


If the script was able to connect, it will print something like Resource id #2. If it could not connect, you should see an error message like that in Figure A.25. Most likely this indicates a problem with the MySQL user's privileges (see the previous section of this chapter).

Figure A.25. The script was not able to connect to the MySQL server.


If you see an error like in Figure A.26, this means that PHP does not have MySQL support enabled. See the next section of this chapter for the solution.

Figure A.26. The script was not able to connect to the MySQL server because PHP does not have MySQL support enabled.


Tips

  • For security reasons, you should not leave the phpinfo.php and mysql_test.php scripts on a live server because they give away too much information.

  • If you run a PHP script in your Web browser and it attempts to download the file, then your Web server is not recognizing that file extension as PHP. Check your Apache (or other Web server) configuration to correct this.

  • PHP scripts must always be run from a URL starting with http://. They cannot be run directly off a hard drive (as if you had opened it in your browser).

  • If a PHP script cannot connect to a MySQL server, it is normally because of a permissions issue. Double-check the username, password, and host being used, and be absolutely certain to flush the MySQL privileges.




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net