php连接mssql数据库的几种方式
数据库查询不外乎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建立连接代码。
<span style="color: #800080">$con </span>= <span style="color: #008080">odbc_connect</span>('odbc名称','用户名','密码');
2、连接mssql2000
<span style="color: #800080">$con</span> = mssql_connect('<span style="color: #800080">数据库地址</span>','用户名','密码');
3、连接mssql2008
<span style="color: #800080">$connectionInfo</span> = <span style="color: #0000ff">array</span>("UID"=><span style="color: #800080">用户名</span>,"PWD"=><span style="color: #800080">密码</span>,"Database"=>"数据库名称"<span style="color: #000000">); </span><span style="color: #800080">$con</span> = sqlsrv_connect( <span style="color: #800080">数据库地址</span>,<span style="color: #800080">$connectionInfo</span>);
二、输入查询代码
这个都一样,可以直接写入,也可以从mssql中验证好后复制过来。简单点说就是把一个sql语句赋值给一个变量。
类似下面代码
<span style="color: #800080">$query</span> = "SELECT top 12 * 数据库名称 order by id desc";
三、建立查询并取出数据
1、odbc
<span style="color: #800080">$result</span> = <span style="color: #008080">odbc_do</span>(<span style="color: #800080">$con</span>,<span style="color: #800080">$query</span><span style="color: #000000">); </span><span style="color: #0000ff">while</span>(<span style="color: #008080">odbc_fetch_row</span>(<span style="color: #800080">$result</span><span style="color: #000000">)) { </span><span style="color: #800080">$变量名称</span> = <span style="color: #008080">odbc_result</span>(<span style="color: #800080">$result</span>, "字段名称")<span style="color: #000000">; }</span>
2、连接mssql2000
<span style="color: #800080">$result</span> = mssql_query(<span style="color: #800080">$con</span>, <span style="color: #800080">$query</span><span style="color: #000000">); </span><span style="color: #0000ff">while</span>(<span style="color: #800080">$row</span> =mssql_fetch_array(<span style="color: #800080">$result</span><span style="color: #000000">)) { </span><span style="color: #800080">$变量名称</span> = <span style="color: #800080">$row</span>["字段名称"<span style="color: #000000">]; }</span>
3、连接mssql2008
<span style="color: #800080">$result</span> = sqlsrv_query(<span style="color: #800080">$con</span>, <span style="color: #800080">$query</span><span style="color: #000000">); </span><span style="color: #0000ff">while</span>(<span style="color: #800080">$row</span> = sqlsrv_fetch_array(<span style="color: #800080">$result</span><span style="color: #000000">)) { </span><span style="color: #800080">$变量名称 </span>= <span style="color: #800080">$row</span>["字段名称"<span style="color: #000000">]; }</span>
在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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

The future of C will focus on parallel computing, security, modularization and AI/machine learning: 1) Parallel computing will be enhanced through features such as coroutines; 2) Security will be improved through stricter type checking and memory management mechanisms; 3) Modulation will simplify code organization and compilation; 4) AI and machine learning will prompt C to adapt to new needs, such as numerical computing and GPU programming support.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

C isnotdying;it'sevolving.1)C remainsrelevantduetoitsversatilityandefficiencyinperformance-criticalapplications.2)Thelanguageiscontinuouslyupdated,withC 20introducingfeatureslikemodulesandcoroutinestoimproveusabilityandperformance.3)Despitechallen

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.
