ADODB supported databases include MySQL, Postgresql,interbase,firebird,informix,oracle,ms SQL 7,foxpro,access,ado,sybase,frontbase,db2 and generic ODBC.
Installation of ADODB
Installing ADODB is a very easy thing to do, and I believe you will not feel the difficulty of being smart.
First, make sure that the PHP you are running is version 4.0.4 or newer. If not, I strongly recommend you upgrade!
Download the. zip or. tgz file from the PHP Everywhere site and unzip it to the path you selected.
This path should not be in the Web directory (Wwwtree, translator Note: If your page is under/www/, then this directory should not be/www/here) under! Although ADODB's include file already uses the. inc.php extension, the server does not display these. inc files in the browser in plaintext, even under the worst-case configuration, but we have always discouraged the behavior of placing library function files in the Web directory. Then the downloaded files run: TAR-ZXVF adodb350.tgz decompression, under Windows you can use a decompression software you like to operate, so that you will get a ADODB directory under which there are many subdirectories.
Test your installation
OK, let's Test your installation. Test whether your installation was successful by adding the following three lines of code to the script. Be careful to change the parameters in your code to your own.
Include ("$adodb _path/adodb.inc.php"); Includes the ADODB library
$db = Newadoconnection (' $database _type '); A New Connection
$db->connect ("$host", "$user", "$password", "$database _name");
Now that you have a database connection object $db. You can also use adonewconnection to replace newadoconnection--, which are different names for the same function. Connected Database variables $database _type needs to be changed to your situation. You can use one of the following lists (in parentheses, in the Description section, not in your code):
Access (Microsoft Access/jet)
ADO (Generic ado, the base for all of the other ADO drivers)
Ado_access (Microsoft Access/jet using ADO)
Ado_mssql (Microsoft SQL Server using ADO)
DB2 (DB2)
VFP (Microsoft Visual FoxPro)
Fbsql (Frontbase)
IBase (Interbase 6 or before)
Firebird (Firebird)
informix72 (Informix databases before Informix 7.3)
Informix (Informix)
Maxsql (MySQL with transaction support)
MSSQL (Microsoft SQL Server 7)
Mssqlpo (Portable MSSQL driver)
MySQL (MySQL without transaction support)
MYSQLT (MySQL with transaction support, identical to Maxmysql)
Oci8 (Oracle 8/9)
oci805 (Oracle 8.0.5)
Oci8po (Oracle 8/9 Portable driver)
ODBC (Generic ODBC, the base for all of the other ODBC drivers)
Odbc_mssql (MSSQL via ODBC)
Odbc_oracle (Oracle via ODBC)
Oracle (Oracle 7)
Postgres (PostgreSQL)
POSTGRES64 (PostgreSQL 6.4)
Postgres7 (PostgreSQL 7, currently identical to Postgres)
Sqlanywhere (Sybase SQL Anywhere)
Sybase (Sybase)
If your link code has an incorrect hint, the first thing you need to check is on the path or the connected variable. Before you blame ADOdb, make sure you are using those variables correctly. (Many friends Changhua too much time to fix these obvious mistakes.) If the connection does not have any error prompts, we can now use ADODB in our project.
Connect to the database through your script
Before adding the above code to your code, let's take a step back. We'd better wrap up the above code in our own way. This will make your program more flexible and more portable. If you insert the above code directly into each file of your project, if the path of the project changes in the future, it will be very easy to create an error, if your password changes, you may need to modify all of your scripts, which will affect the purpose of our use of library functions. And, because your password information is under the Webtree, this will create a hidden danger. I recommend placing the password information in a separate include file, such as somewhere in the ADOdb installation directory. If you want to run your program on a different server, you cannot guarantee that the directory structure will be the same, so you have to make sure that the path is correct. I recommend using PHP's auto-include feature to automatically include this file.
Include ("$adodb _path/db_values.inc.php");
Include ("$adodb _path/adodb.inc.php");
$db = Newadoconnection (' $database _type ');
$db->connect ("$host", "$user", "$password", "Employees");
If you also want to use persistent connections, do not create a new connection every time (this makes many Web applications faster, but be aware that some databases are not supported). You can replace Connect with Pconnect.
File db_values.inc.php is our database information file (you need to change the variables in the following code to your own):
<?php
$database _type= "MySQL";
$host = "localhost"; Local Database
$user = "Ian" 2881064151
$password = "Let_me_in"
?>
You can set the automatic inclusion of our configuration file in the php.ini configuration, and we can modify the following line of PHP.ini:
; Automatically add files before or after any PHP document.
Auto_prepend_file =/usr/local/build/apache/www/tool_lib/defaults.inc
Auto_append_file =
The file Defaults.inc contains the value of the $ADBDB _path:
<?
$adodb _path = "/usr/local/build/apache/www/tool_lib/";
?>
There are other ways to implement it, but I have found that this approach can be relatively less complex when porting.
Choose from a database (select) operation
There are several ways to access a database when you are using well-developed library functions, as well as functions provided by PHP itself. What you do with it depends entirely on your own preferences.
Here is a simple example:
$sql = "Select surname, age from Employees";
$rs = & $db->execute ($sql);
if (! $rs) {
Print $db->errormsg (); Displays the error message if no results could be returned
}
else {
while (! $rs->eof) {
Print $rs->fields[0]. ' '. $rs->fields[1]. ' <BR> ';
Fields[0] is surname, Fields[1] was age
$rs->movenext (); Moves to the next row
}
}
In the example above, $rs->fields is an array that contains the return value. The array index is given the initial number, and you can specify its index as follows:
$sql = "Select surname, age from Employees";
$db->setfetchmode (ADODB_FETCH_ASSOC); Return Associative array
$rs = & $db->execute ($sql);
if (! $rs) {
Print $db->errormsg (); Displays the error message if no results could be returned
}
else {
while (! $rs->eof) {
Print $rs->fields[' surname ']. " ". $rs->fields[' age ']." <BR> ";
$rs->movenext (); Moves to the next row
}//End While
}//End Else
Another optional method for result browsing is to return each record as an object. ADOdb has a fetchnextobject () function to implement this function, and the pointer moves automatically to the next record.
$sql = "Select surname, age from Employees";
$db->setfetchmode (ADODB_FETCH_ASSOC); Return Associative array
$rs = & $db->execute ($sql);
if (! $rs) {
Print $db->errormsg (); Displays the error message if no results could be returned
}
Loop through results
while ($row = $rs->fetchnextobject ()) {
The field names need to be uppercase
Print $row->surname. " ". $row->age." <BR> ";
}
Inserting, updating records
The basic INSERT operation is convenient and quick, with the same syntax as SELECT.
$sql = "INSERT into employees (surname, age) VALUES (' Clegg ', ' 43 ')";
if (! ( $db->execute ($sql))) {
print ' Error inserting: '. $db->errormsg (). ' <BR> ';
}
The real advantage of library functions is that it allows you to put records into different databases through the same syntax, which is absolutely impossible in the past. There are usually two situations that occur.
The first type, the quotation marks. All quotes need to be substituted with the caret (that is, the ' symbol ', the key is on the top of the tab), or it will cause a syntax error. But some databases use one single quotation mark, others use two single quotes. Therefore, you should use QSTR () in ADOdb instead of addslashes () in PHP. In this way, the return value will match the database you are using.
The second type, the date. Many databases accept incompatible formats that are inconsistent with their date types. ADOdb has a DBDate () function that can convert the Unix timestamp, or ISO (y-m-d) format to any format, to meet the needs of your database. See the following example:
$employee _surname = $db->qstr ("d ' Angelo");
$arrival _time = $db->dbdate (time ());
The above functions also add the enclosing quotes, so, $arrival _time, not ' $arrival _time '
$sql = "INSERT into Employee_arrival (arrival_time,surname) VALUES ($arrival _time, $employee _surname)";
if (! ( $db->execute ($sql))) {
print ' Error inserting: '. $db->errormsg (). ' <BR> ';
}
You can update the database in exactly the same way, for example:
$sql = "UPDATE employees SET age= ' WHERE id= ' 121 ')";
if (! ( $db->execute ($sql))) {
print ' Error updating: '. $db->errormsg (). ' <BR> ';
}
What are the databases supported by ADODB?