search
HomeBackend DevelopmentPHP TutorialHow to connect to MSsql server using php

This time I will show you how to use php to connect to MSsql server, and what are the precautions to use php to connect to MSsql server. The following is a practical case, let's take a look.

1. Through mssql_ series functions

mssql_ series functions are mainly used for versions below php5.3 and sqlserver2000 and above.

In php.ini, remove the semicolon in front of;extension=php_mssql.dll;extension=php_msql.dll;extension=php_pdo_mssql.dll;extension=php_pdo_odbc.dll.

<?php  $server ="localhost"; //服务器IP地址,如果是本地,可以写成localhost
 $uid ="sa"; //用户名
 $pwd ="123456"; //密码
 $database ="jb51net"; //数据库名称
 
////进行数据库连接
 $conn =mssql_connect($server,$uid,$pwd) or die ("connect failed");
 mssql_select_db($database,$conn);
 
////执行查询语句
 $query ="select * from A_PHP";
 $row =mssql_query($query);
 
////打印输出查询结果
 while($list=mssql_fetch_array($row))
 {
    print_r($list);
    echo "<br>";
 }
?>

2. Through the sqlsrv_ series of functions

sqlsrv_ series of functions are mainly aimed at php5.3 or above and sql server 2005 or above use.

You need to download and install the Microsoft Drivers for PHP for SQL Server driver, address:

https://msdn.microsoft.com/library/dn865013.aspx. After downloading, unzip it and put it in the ext directory corresponding to php. Then open the php.ini file and add the configuration after extension

extension=php_pdo_sqlsrv_53_ts.dll

extension=php_sqlsrv_53_ts.dll

Restart apache and check

phpinfo( ), make sure apache already supports sqlsrv. As shown in the figure below:

And install sqlncli.msi. This file is to assist

windows environmentto access the database server where the sql server is located

<?php  $serverName = "localhost";
 $connectionInfo = array( "Database"=>"jb51net", "UID"=>"sa", "PWD"=>"123456");
 $conn = sqlsrv_connect( $serverName, $connectionInfo );
 if( $conn === false ) {
   die( print_r( sqlsrv_errors(), true));
 }
 $sql = "SELECT * FROM dbo.A_PHP";
 $stmt = sqlsrv_query( $conn, $sql );
 if( $stmt === false) {
   die( print_r( sqlsrv_errors(), true) );
 }
 while($row = sqlsrv_fetch_array($stmt))
 {echo $row[0]."-----".$row[1]."<br>";}
?>

3. Connect the sqlserver series through odbc method.

You need to enable the php_pdo_odbc.dll extension in php.ini.

<?php $con = odbc_connect(&#39;DRIVER={SQL Server};SERVER=localhost;DATABASE=jb51net,&#39;sa&#39;,&#39;123456&#39;);
$query = "SELECT * FROM dbo.A_PHP";
$result = odbc_do($con,$query);
while(odbc_fetch_row($result))
{
  $list = odbc_result($result, "id");	print_r($list);echo &#39;<br/>';
}
?>

4. Connect to sqlserver through PDO.

Enable the php_pdo_mssql.dll extension in php.ini. You can view

<?php  $conn = new PDO("sqlsrv:server=localhost;database=jb51net","sa","123456");
 $sql = "select count(*) count from CKXS2";
 $res = $conn->query($sql);
 while ($row = $res->fetch()){
 print_r($row);
 }
?>

in phpinfo. 5. Connect through COM.

The following is the implementation code:

<?php  $conn = new Com("ADODB.Connection"); //实例化一个Connection对象 
 $connstr = "provider=sqloledb;datasource=.;uid=sa;pwd=123456;database=jb51net;"; 
 $conn->Open($connstr); 
 $rs = new Com("ADODB.Recordset"); //实例化一个Recordcount对象 
 $rs->Open('select * from CKXS2 ', $conn, 1, 1); 
 $count = $rs->RecordCount; 
 echo "共有{$count}条纪录<br>"; 
?>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Forced downloading of QR code images

##Build a PHP development environment in Docker

The above is the detailed content of How to connect to MSsql server using php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么连接mssql数据库php怎么连接mssql数据库Oct 23, 2023 pm 12:02 PM

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。详细介绍:1、使用PHP的MSSQL扩展方法,确保PHP安装了MSSQL扩展。可以在PHP配置文件(php.ini)中检查是否启用了mssql扩展;2、使用PDO方法,确保PHP安装了PDO扩展。可以在PHP配置文件(php.ini)中检查是否启用了pdo_sqlsrv扩展。

Ubuntu安装PHP并配置MSSQL连接的详细指南Ubuntu安装PHP并配置MSSQL连接的详细指南Feb 29, 2024 am 11:15 AM

Ubuntu是一款流行的开源操作系统,常用于服务器运行。在Ubuntu上安装PHP并配置MSSQL连接是许多开发者和系统管理员经常需要做的操作之一。本文将为读者提供一份详细的指南,步骤包含安装PHP、设置Apache、安装MSSQLServer等内容,并附上具体的代码示例。第一步:安装PHP及相关扩展首先,我们需要安装PHP及其相关扩展,以便支持PHP连接

如何安装、卸载、重置Windows服务器备份如何安装、卸载、重置Windows服务器备份Mar 06, 2024 am 10:37 AM

WindowsServerBackup是WindowsServer操作系统自带的一个功能,旨在帮助用户保护重要数据和系统配置,并为中小型和企业级企业提供完整的备份和恢复解决方案。只有运行Server2022及更高版本的用户才能使用这一功能。在本文中,我们将介绍如何安装、卸载或重置WindowsServerBackup。如何重置Windows服务器备份如果您的服务器备份遇到问题,备份所需时间过长,或无法访问已存储的文件,那么您可以考虑重新设置WindowsServer备份设置。要重置Windows

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.