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