How It Works (Personal web server) Your first file, config.php, contains

How It Works Your first file, config.php, contains just five lines: define( SQL_HOST , localhost ); define( SQL_USER , bp5am ); define( SQL_PASS , bp5ampass ); define( SQL_DB , comicsite ); define( ADMIN_EMAIL , your@emailaddress.com ); These constants are created separately so that in the event that you make a change such as moving the application to another server, or changing the password, it can be done in only one location. If this data were in each file, any change would require modification of several files. Include config.php in each file that requires access to MySQL like so: require( config.php ); You use require() instead of include() because if the file is not loaded, you want to halt loading of the page. Another option would be to use include(), and then immediately test for the existence of one of the constants. If it does not exist, you could then redirect the user to another page, which makes for a more user-friendly experience. As you can see here, the constants from config.php are used to make your connection: $conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die( Could not connect to MySQL database. . mysql_error()); $sql = CREATE DATABASE IF NOT EXISTS . SQL_DB; $res = mysql_query($sql) or die(mysql_error()); mysql_select_db(SQL_DB, $conn); Take notice of the CREATE DATABASE statement. There are a couple of things you should be aware of about creating databases. If you already have a database (one that you created in another chapter, or if your site is already using a database), it s better to keep your mailing list tables in that database. You don t need extra databases because if your Web site spans multiple databases, you will need to create multiple connections to those databases, and that is extra overhead that is just not necessary. In addition, if there are any relationships between your tables from this application and tables from other apps, they need to be in the same database. For example, if you have a user table that stores your registered users for all applications, all of the applications that rely on that user table should reside in the same database. If you do need to create a new database, the CREATE DATABASE command may still not work if your site is hosted by an Internet service provider (ISP). Some ISPs don t allow programmed creation of databases. If this is the case, you may need to go through your ISP to create the database (through PHPMyAdmin, for example) and run sql.php. Just be sure to put the proper database name in the code. See Chapter 10 for more information about creating databases. 484 Chapter 14
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Leave a Reply