_php instance of a code instance that uses ADODB to connect different databases in a PHP program

Source: Internet
Author: User
Tags db2 db2 odbc driver ibm db2 ibm db2 odbc driver odbc php programming postgresql oracle database

adodb.inc.php files need to download a ADODB, put them in the appropriate location, and then include them in PHP.


Connecting Oracle Databases

 <?php 
  include ("adodb/adodb.inc.php");         Contains the ADODB class library file 
   $conn = &newadoconnection (' oci8 ');
   $conn->connect ($DBServer, $DBUser, $DBPass);  
  > 

Connecting to the PostgreSQL database

  <?php 
  include ("adodb/adodb.inc.php");         Contains the ADODB class library file 
  $conn = newadoconnection (' Postgres '); 
  $conn->connect (' localhost ', ' root ', ' pass ', ' mydb ');    
  

Connecting to the PostgreSQL database

  <?php 
  include ("adodb/adodb.inc.php");           Contains the ADODB class library file 
  $conn = newadoconnection (' Postgres '); 
  $conn->connect (' host=localhost port=5432 dbname=mydb '); Connecting to the PostgreSQL database 

  ?>

Connecting to the MySQL database

  <?php 
  include ("adodb/adodb.inc.php");                 Contains the ADODB class library file 
  $conn = newadoconnection (' mysql://root:1981427@localhost/test ');      Connecting to MySQL database 
  ?> 

Connecting to the MySQL database

  <?php 
  include ("adodb/adodb.inc.php");         Contains the ADODB class library file 
  $conn = newadoconnection (' mysql ');        
  $conn->connect (' localhost ', ' root ', ' 1981427 ', ' test ');   Connecting to MySQL database 
  ?> 

Connect to an 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)};D Bq=d://mydb.mdb; Uid=admin; pwd=; "); 
  ? > 

Connecting to SQL Server databases via ODBC

  <?php 
  include ("adodb/adodb.inc.php");         Contains the ADODB class library file 
  $conn =newadoconnection (' Odbc_mssql ');      Connect the SQL Server database 
  $conn->connect ("Driver={sql Server}"; Server=localhost;database=mydb; ", ' username ', ' password '); 
  ? > 


Connecting to SQL Server databases

  <?php 
  include ("adodb/adodb.inc.php");         Contains ADODB class library files 
  $conn =newadoconnection (' mssql ');        Connect SQL Server database 
  $conn->connect ("localhost ', ' username ', ' password ', ' mydb '); 
  ? > 

Connecting to the DB2 database

  <?php 
  include ("adodb/adodb.inc.php");         Contains the ADODB class library file 
  $conn =newadoconnection (' DB2 ');         Connect the DB2 database 
  $conn->connect ("DRIVER={IBM DB2 ODBC driver};d atabase=mydb;hostname=localhost;port=50000; 
   Protocol=tcpip;uid=root; Pwd=pass "); 
  ? > 

Here's an example of a PHP programming application:
First define a database connection function, support a variety of different databases, pass in an SQL statement, return the results of the execution, 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;D atabase= $DBName; Uid= $DBUser;
   Pwd= $DBPass; ";
   Create SQL Server database connection $conn = &newadoconnection (' Odbc_mssql '); $conn->connect ("Driver={sql Server}"; Server= ". $DBServer.";D Atabase= ". $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 Oracle database connection $conn = &newadoconnection (' oci8 ');
  $conn->connect ($DBServer, $DBUser, $DBPass);
  Break Case 2://access//$StrCon = "Driver={microsoft Access Driver (*.mdb)};D bq= $DBName; Uid= $DBUser;
   Pwd= $DBPass; ";
   Create an Access database connection $conn = &newadoconnection (' access '); $conn->connect ("Driver={microsoft Access Driver (*.mdb)};D bq=". $DBName. "; Uid= ". $DBUser."; Pwd= ". $DBpass. ";");
  Break
   Case 3://mysql//create MySQL database connection $conn = newadoconnection (' mysql ');
   $conn->connect ($DBServer, $DBUser, $DBPass, $DBName); $conn = &newadoconnection ("mysql://". $DBUser. ":". $DBPass. " @ ". $DBServer." 
  /". $DBName);
  Break Default:echo "Sorry, do not support this database, please check the configured database type!"
  ";
  Die (); 
 Break
 //Execute query operation $result = $conn->execute ($SQLSTR);
 Database query Failure processing if ($result ==false) {die ("Query failed");
  //Output query results 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;
 Closes the database connection $conn->close ();
 return $output;
 }

Define a function that indicates the type of database you want to use, the database username and password, and so on, incoming SQL statements, calls the database connection method defined above, and executes the SQL statement.

 Public Function ExecuteSQL ($sqlStr)
 {
  $DBType =1;
  $DBServer = "Arron";
  Database username, do not use sys this superuser
  $DBUser = "System";
  $DBPass = "ABCDE";
  $DBName = "";
  $SQLSTR = "SELECT * from Func;";
  $MYEXPECTSTR = $this->dbconnect ($DBType, $DBServer, $DBUser, $DBPass, $DBName, $sqlStr);
  return $myExpectStr;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.