THINKPHP5 Accessing the SQL2000 database

Source: Internet
Author: User
Tags dsn odbc php and mysql

We all know that PHP and MySQL is perfect, but because sometimes work requires PHP access operation sql2000, how to do?

There are generally two ways of doing this:

1. sqlsrv Drive Mode

2. ODBC mode

Sqlsrv drive mode, because Microsoft company reason, does not provide sql2000 access driver, only provides sql2005 above the driver, so if your database is more than 2005 version, especially SQL2008R2, to Microsoft Official can download to sql2008 for PHP driver, there are many articles on the internet, my blog has an article specifically discuss the situation;

My working environment is: PHP 5.5n (phpstudy) + nginx + tp5.10 + sql2000 + win7 (64-bit) system

Online Search This article, tested, the article method is available:

(i) Working in the controller using ODBC:

<?phpnamespace App\index\controller; UsePDO;//For PDO ODBC sql2000 or SQL2008R2classindex{ Public functionindex () {Header(' content-type:text/html; Charset=utf-8 '); ////////////////////////////////////////////////////////test sql2000&sql2008r2 PDO        //////////////////////////////////////////////////////        $dbname= ' Master '; $username= ' sa '; $password= ' Yoooko '; //--------------------------------------------------------------OK//sql2000        $mssqldriver= ' {SQL Server} '; $hostname= '\sql2000,1434 '; //Connect using ODBC mode        $dbDB=NewPDO ("odbc:driver=$mssqldriver; Server=$hostname;D atabase=$dbname",$username,$password); //--------------------------------------------------------------                //--------------------------------------        ------------------------OK//sql2008r2//$mssqldriver = ' {SQ Server Native Client 11.0} ';        $mssqldriver = ' {ODBC Driver one for SQL Server} ';        $hostname = '\sql2008r2,14333 '; Not available when sqlsrv driver is installed, error:could not find driver//$dbDB = new PDO ("sqlsrv:server= $hostname;D atabase= $dbname", $user        Name, $password); //--------------------------------------------------------------        $sql= "SELECT * FROM CS"; foreach($dbDB->query ($sql) as $row) {            Var_dump($row); }    }}

(ii) Working with the database.php + controller

Configuration in database.php:

return [    //  database type    ' type '            // must be entered <br>//    user name    ' Username '        = ' sa ',    / / password    ' password '        = ' Yoooko ',    //  Connect DSN, driver, server address and port, database name    ' DSN '             = ' odbc:driver={sql server}; Server=\sql2000,1434;database=master ',];

In the index.php controller:

<?phpnamespace App\index\controller; Usethink\db;classindex{ Public functionindex () {Header(' content-type:text/html; Charset=utf-8 '); //$user = db::table (' cs ')->select (); No, there's a row_number () error        $user= Db::query ("SELECT * from CS");//must use native SQL, correct        Echo' <pre> '; Print_r($user); Echo' </pre> '; }}

THINKPHP5 Accessing the SQL2000 database

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: 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.