This article mainly introduces the code instances for connecting to different databases using adodb in PHP programs. the specific usage example uses the switch statement to write a summary support. For more information, see adodb. inc. the php file needs to download an adodb file, put it in the corresponding location, and then include it in php.
Connect to oracle Database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = & NewADOConnection ('ci8'); $ conn-> Connect ($ DBServer, $ DBUser, $ DBPass);?>
Connect to the PostgreSQL database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('codecs'); $ conn-> Connect ('localhost', 'root', 'pass ', 'mydb');?>
Connect to the PostgreSQL database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('s S'); $ conn-> Connect ('host = localhost port = 5432 dbname = mydb '); // connect to the PostgreSQL database?>
Connect to the Mysql database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('MySQL: // root: 1981427 @ localhost/test'); // connect to the mysql database?>
Connect to the MySQL database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('mysql'); $ conn-> Connect ('localhost', 'root', '123 ', 'test'); // connect to the MySQL database?>
Connect to the Access Database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('access '); // Connect to the Access Database $ conn-> Connect ("Driver = {Microsoft Access Driver (*. mdb)}; Dbq = d: // mydb. mdb; Uid = Admin; Pwd =; ") ;?>
Connect to the SQL Server database and use ODBC
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('odbc _ mssql '); // Connect SQL Server Database $ conn-> Connect ("Driver = {SQL Server}; Server = localhost; Database = mydb;", 'username ', 'password');?>
Connect to the SQL Server database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('mssql'); // connects to the SQL Server database $ conn-> Connect (" localhost', 'username ', 'password', 'mydb');?>
Connect to the DB2 database
<? Php include ("adodb/adodb. inc. php "); // contains the adodb class library file $ conn = NewADOConnection ('db2 '); // Connect to the DB2 database $ conn-> Connect ("driver = {IBM db2 odbc DRIVER}; database = mydb; hostname = localhost; port = 50000; protocol = TCPIP; uid = root; pwd = pass ");?>
Here is an example of PHP programming:
First, define a database connection function that supports multiple different databases. input an SQL statement to return the execution result. the code is as follows:
Public function DBConnect ($ DBType, $ DBServer, $ DBUser, $ DBPass, $ DBName, $ sqlStr) {$ StrCon; $ result; $ conn; $ output = ""; switch ($ DBType) {Case 0: // SQL Server // $ StrCon = "Driver = {SQL Server}; Server = $ DBServer; Database = $ DBName; uid = $ DBUser; Pwd = $ DBPass; "; // Create an SQL Server database connection $ conn = & NewADOConnection ('odbc _ mssql '); $ conn-> Connect ("Driver = {SQL Server}; Server = ". $ DBServer. "; Database = ". $ DBName. "; Uid = ". $ DBuser."; Pwd = ". $ DBPass. ";"); break; Case 1: // Oracle // $ StrCon = "Provider = MSDAORA.1; Data Source = $ DBServer; Password = $ DBPass; User ID = $ DBUser; persist Security Info = True; "; // Create an oracle database connection $ conn = & NewADOConnection ('ci8'); $ conn-> Connect ($ DBServer, $ DBUser, $ DBPass); break; Case 2: // Access // $ StrCon = "Driver = {Microsoft Access Driver (*. mdb)}; Dbq = $ DBName; Uid = $ DBUser; Pwd = $ DBPass; "; // Create an Access database connection $ conn = & NewA DOConnection ('access'); $ conn-> Connect ("Driver = {Microsoft access Driver (*. mdb)}; Dbq = ". $ DBName. "; Uid = ". $ DBUser. "; Pwd = ". $ DBPass. ";"); break; Case 3: // mysql // Create a mysql database connection $ conn = NewADOConnection ('mysql'); $ conn-> Connect ($ DBServer, $ DBUser, $ DBPass, $ DBName); // $ conn = & NewADOConnection ("mysql ://". $ DBUser. ":". $ DBPass. "@". $ DBServer. "/". $ DBName); break; default: echo "Sorry, this database is not supported for the moment. check the configured database type! "; Die (); break;} // Execute the query operation $ result = $ conn-> Execute ($ sqlStr ); // if ($ result = false) {die ("query failed");} // output the query result while (! $ Result-> EOF) {$ max = $ result-> fieldcount (); // $ output = array (); for ($ I = 0; $ I <$ max; $ I ++) {// echo $ result-> fields [$ I]. ''; $ res [$ I] = $ result-> fields [$ I]. ''; $ output = $ output. $ res [$ I] ;}$ result-> MoveNext () ;}$ output = trim ($ output); // echo $ output; // Close the database connection $ conn-> Close (); return $ output ;}
Define a function that specifies the database type, database username, and password to be used, input an SQL statement, call the database connection method defined above, and execute an SQL statement.
Public function ExecuteSql ($ sqlStr) {$ DBType = 1; $ DBServer = "arron"; // database Username. do not use sys as the superuser $ DBUser = "system "; $ DBPass = "abcde"; $ DBName = ""; // $ sqlStr = "SELECT * FROM func;"; $ myExpectStr = $ this-> DBConnect ($ DBType, $ DBServer, $ DBUser, $ DBPass, $ DBName, $ sqlStr); return $ myExpectStr ;}}