Using ODBC in PHP to connect to a SQL Server database

Source: Internet
Author: User
This article is about the use of ODBC in PHP to connect to the SQL Server database, has a certain reference value, now share to everyone, the need for friends can refer to

I use: wampserver integration tool, PHP version 7.0.29, database for SQL Server R2;

Note: When connecting to the database, you need to enter the PHP configuration file under the X:\wamp\bin\apache\apache2.4.33\bin directory: php.ini. Enter inside to modify the relevant configuration. The configuration is as follows:

Extension=php_odbc.dll remove the front colon to open the service and restart the next wampserver all services. (restarting Apache is OK)

I use the thinkPHP5.1 framework to configure it.

Create a Test.class.php in the controller

The code is as follows:


Use Think\controller;class Test extends controller{public        function zz () {            $servername = ' Driver={sql Server}; Server=localhost;database=mysql ';            $username = ' sa ';            $password = ' 123456 ';            $conn = Odbc_connect ($servername, $username, $password);            $sql = "SELECT * from MYLLP";            $exec =odbc_exec ($conn, $sql);            while ($row =odbc_fetch_array ($exec)) {                $List []= $row;            }            echo Json_encode ($List);            Exit;        }}

Where the database name is: MySQL.


The final effect is mainly to show the database MySQL inside the table MYLLP data, in the form of key-value pairs on the page display.

By accessing the URL address, the Portal file + module + Controller + method

The page path is: Http://localhost:81/tp5/public/index/test/zz

The page output is:[{"Name": "LLP", "Password": "123", "id": "1"},{"name": "BB", "Password": "123", "id": "2"}]

Related recommendations:

PHP Connection under Windows Oracle Configuration

PHP connection to MySQL method-mysqli and PDO

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.