php连接MSSQL数据库的常用操作
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 数据库查询不外乎4个步骤,1、建立连接。2、输入查询代码。3、建立查询并取出数据。4、关闭连接。 php连接mssql数据库有几个注意事项,尤其mssql的多个版本、32位、64位都有区别。 首先,php.ini文件
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
数据库查询不外乎4个步骤,1、建立连接。2、输入查询代码。3、建立查询并取出数据。4、关闭连接。
php连接mssql数据库有几个注意事项,尤其mssql的多个版本、32位、64位都有区别。
首先,php.ini文件中;extension=php_pdo_mssql.dll ;extension=php_pdo_odbc.dll 前面的分号去掉,对应的使哪种方式连接mssql.注意要重启服务使其生效。
一、建立连接
1、odbc
首先,在php程序所在的服务器设置odbc.这里32位和64位操作系统有区别。32位的从控制面板中管理工具中的数据源(odbc)直接建立就可以了,64位的要运行C:\Windows\SysWOW64\odbcad32.exe
从这里面设置。注意:上面只的是数据库服务器为32为的,数据源设置服务器为32位和64位两种的情况。只要两个服务器建立的数据源位数一致就好。
下面是odbc建立连接代码。
$con = odbc_connect('odbc名称','用户名','密码');
2、连接mssql2000
$con = mssql_connect('数据库地址','用户名','密码');
3、连接mssql2008
$connectionInfo = array("UID"=>用户名,"PWD"=>密码,"Database"=>"数据库名称");
$con = sqlsrv_connect( 数据库地址,$connectionInfo);
二、输入查询代码
这个都一样,可以直接写入,也可以从mssql中验证好后复制过来。简单点说就是把一个sql语句赋值给一个变量。
类似下面代码
$query = "SELECT top 12 * 数据库名称 order by id desc";
三、建立查询并取出数据
1、odbc
$result = odbc_do($con,$query);
while(odbc_fetch_row($result))
{
$变量名称 = odbc_result($result, "字段名称");
}
2、连接mssql2000
$result = mssql_query($con, $query);
while($row =mssql_fetch_array($result))
{
$变量名称 = $row["字段名称"];
}
3、连接mssql2008
$result = sqlsrv_query($con, $query);
while($row = sqlsrv_fetch_array($result))
{
$变量名称 = $row["字段名称"];
}
在php5.3及以后的版本中不附带sqlsrv库了。所以要从微软这里下载。
四、关闭连接
这个没有什么区别,分别是odbc_close();和mssql_close()和sqlsrv_close();
最后体会:php连接mssql比连接mssql的函数少了一些,但是也够用了。具体函数可以参考php官方手册或者oschina的php中文文档。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
