Home > Article > Backend Development > Use ODBC to connect to SQL Server database in PHP
The content of this article is about using ODBC in PHP to connect to the SQL Server database. It has certain reference value. Now I share it with you. Friends in need can refer to it
I use: WampServer integration tool, PHP version is 7.0.29, database is SQL Server 2008 R2;
Note: When connecting to the database, You need to enter the PHP configuration file: PHP.ini in the X:\wamp\bin\apache\apache2.4.33\bin directory. Go inside and modify the relevant configuration. The configuration is as follows:
##extension=php_odbc.dll Remove the colon in front to start the service and restart all WampServer services. (Restarting Apache will also work)
I configured it using the thinkPHP5.1 framework.
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; }}
The database name is: mysql.
The final effect is mainly to display the data of table myLLP in the database mysql, which is displayed on the page in the form of key-value pairs.
By accessing the url address, the entry 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 to Oracle configuration under windows
PHP connection to mysql method-mysqli and PDO
The above is the detailed content of Use ODBC to connect to SQL Server database in PHP. For more information, please follow other related articles on the PHP Chinese website!