Code instances in PHP programs that use ADODB to connect to different databases, _php tutorial

Source: Internet
Author: User
Tags db2 odbc driver ibm db2 ibm db2 odbc driver mssql odbc connection

PHP programs use ADODB to connect to different database code instances,


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


Connecting to an Oracle database

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

Connect to PostgreSQL Database

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

Connect to 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 the PostgreSQL database   ?>

Connect to MySQL Database

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

Connect to MySQL Database

  <?php   include ("adodb/adodb.inc.php");         Contains the ADODB class library file   $conn = newadoconnection (' mysql ');          $conn->connect (' localhost ', ' root ', ' 1981427 ', ' test ');   Connect 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 a SQL Server database through an ODBC connection

  <?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 a SQL Server database

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

Connecting 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 ");   

Let's take a look at a sample application in PHP programming:
First, define a database connection function, support a number of different databases, pass in an SQL statement, return the execution results, 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 a 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 an 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, this database is not supported at this stage, check the configured database type!"  ";  Die ();  Break }//Perform query operation $result = $conn->execute ($SQLSTR);  Database query failed to handle if ($result ==false) {die ("Query Failed");//output 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 database connection $conn->close (); return $output; }

Define a function that indicates the type of database to use, the database user name and password, incoming SQL statements, call the database connection method defined above, and execute the SQL statement.

Public Function ExecuteSQL ($sqlStr) {  $DBType =1;  $DBServer = "Arron";  Database user name, 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; }}

Articles you may be interested in:

    • PHP ADODB connection MSSQL solve garbled problem
    • A collection of common PHP ADODB usage methods
    • ADODB class in PHP

http://www.bkjia.com/PHPjc/1084598.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084598.html techarticle PHP programs use ADODB to connect to different database code instances, adodb.inc.php files need to download a ADODB, put in the appropriate location, and then in PHP include in the. Connect ...

  • Related Article

    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.