I l @ ve RuBoard |
When you worked with text files in Chapter 10, Files and Directories, you saw that you first had to create a file pointer while opening the file. This pointer then acted as a reference point to that open file. A similar process is used when working with databases. First, you have to establish a connection to the database server (in this case, MySQL). This connection will then be used as the access point for any future commands. The MySQL syntax for connecting to a database is: $Link = mysql_connect ("host", "user", "password"); The link is established using three arguments: the host, which is almost always localhost; the user name; and, the password for that user name . These last two parameters will dictate what database permissions you have. Database permissions is a bit more complicated than file permissions but you need to understand this: different types of users can be assigned different database capabilities. For example, a DBMS administrator can create new and delete old databases (within your DBMS you may have dozens of databases), but a lower-level administrator may only be able to create and modify tables within a single database. The most basic user may just be able to read from, but not modify, tables. Your ISP will most likely give you the second type of accesscontrol over a single database but not the DBMS itselfand will establish the initial database for you. If you are working on your own server or have administrative access, you have the capability to create new databases. The code for creating a new database is: mysql_create_db ("databasename", $Link); Notice how the $Link value established when connecting to a database is used to continue working with the database, just as a file pointer is referenced to read from or write to the file. After executing any command to the database it is considered good form to close the link, just as you would close an open file: mysql_close ($Link); For the first database example of this chapter, you will create a new database, which requires that you have administrator access. If your ISP restricts your access, they should create the initial database for you upon request, so you can skip ahead to the next section, Creating a Table. To connect to MySQL and create a new database:
Tip PHP has built-in support for most databases including: dBase, FilePro,mSQL, MySQL, Oracle, PostgreSQL, and Sybase. If you are using a type of database that does not have direct supportfor example, Access or SQL Serveryou'll need to use PHP's ODBC (Open Database Connectivity) functions along with that database's ODBC drivers to interface with the database. See the Database Resources section of Appendix C, PHP Resources, for more information. Tip The combination of using PHP and MySQL is so common now that there are two terms you may run across identifying servers configured with both PHP and MySQL: LAMP ( Linux operating system, Apache Web server, MySQL DBMS, PHP ) and WAMP ( Windows operating system, Apache Web server, MySQL DBMS, PHP ). |
I l @ ve RuBoard |