1. Compile a php class library to connect to the access database for later use and modification, and reuse it multiple times.
The Code is as follows:
<? Php
Class myaccess {
Public $ cn_id; // defines a public variable -- attribute
Function myaccess (){
// Constructor. The initialization object can be executed and connected directly to the database.
$ Drive = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ = ". realpath ("mydb. mdb "). "; Uid = Admin; Pwd = BDC2002Data ;";
// Mydb. mdb is the database file in the same directory. This sentence is used to load the access database driver.
If ($ connect_id = odbc_connect ($ drive, "user_name", "user_pw ")){
// User_name and user_pw are the usernames and passwords used to open the mydb. mdb database. They can be empty.
$ This-> cn_id = $ connect_id;
} Else {
Echo "database connection error! "; // Error message returned for debugging
}
} // End myaccess
Function excu ($ query ){
// Execute the SQL statement
If ($ result = odbc_exec ($ this-> cn_id, $ query )){
Return $ result;
} Else {
Echo "SQL statement execution error! "; // Error message returned for debugging
}
} // End excu
} // Ecn class
?>
Store the above Code in myaccess. php for future calls.
2. Create Database files.
Create a database file named mydb. mdb in the same directory, which contains two fields: the first field is id, the automatic number type, and the second field is text, with a length of 50.
3. Compile a program and call an existing class library. Write the following code and store it in test. php.
The Code is as follows:
<? Php
Include "myaccess. php"; // reference a file
$ Aa = @ new myaccess; // initialize the object
$ Query = "select * from user"; // construct an SQL statement
$ Rst = $ aa-> excu ($ query); // execute an SQL statement
While (odbc_fetch_row ($ rst) {// cyclically output the queried records
$ Id = odbc_result ($ rst, id );
$ Name = odbc_result ($ rst, name );
Echo $ id. "". $ name ."";
}
?>
4. Run the test. php file in the configured service environment to display the result.
5. In another example, store the following code in text2.php.
The Code is as follows:
<? Php
Include "myaccess. php"; // reference a file
$ Aa = @ new myaccess; // initialize the object
$ Query = "select count (*) as num from user"; // construct an SQL statement to query the number of records that meet the conditions
$ Rst = $ aa-> excu ($ query );
$ Total_num = odbc_result ($ rst, num );
Echo $ total_num;
?>
Run the test2.php file in the configured server environment to query the number of records that meet the conditions.
Vi. Additional instructions
If you want to perform some operations to the database, such as inserting records, updating records, and deleting records, you must grant mydb. mdb sufficient access permissions. View the "attributes" of the file, find the "Security" tab, and add the modification and write permissions to the user.
If the "Security" tab is not displayed in the "properties" dialog box, open the "file plus options" dialog box, find the "View" tab, and share "using simple files (recommended).