Home php教程 php手册 基于Linux下PHP连接SQL Server的FreeTDS配置

基于Linux下PHP连接SQL Server的FreeTDS配置

Jun 13, 2016 am 11:12 AM
centos freetds linux php server sql Down based on of connect Configuration

CentOS 5.4 Linux 下的 PHP(FastCGI) 需要连接相关部门的SQL Server 2000数据库,配置了扩展FreeTDS扩展。

1、编译安装FreeTDS

<ol class="dp-xml">
<li class="alt"><span><span>mkdir -p /data0/software/  </span></span></li>
<li><span>cd /data0/software/  </span></li>
<li class="alt"><span>wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz  </span></li>
<li><span>tar zxvf freetds-stable.tgz  </span></li>
<li class="alt"><span>cd freetds-0.82/  </span></li>
<li>
<span>./configure </span><span class="attribute">--prefix</span><span>=/usr/local/webserver/freetds </span><span class="attribute">--with-tdsver</span><span>=</span><span class="attribute-value">8</span><span>.0 --enable-msdblib  </span>
</li>
<li class="alt"><span>make && make install  </span></li>
<li><span>cd ../  </span></li>
<li class="alt"><span> </span></li>
<li>
<span>echo "/usr/local/webserver/freetds/lib/" </span><span class="tag">></span><span> /etc/ld.so.conf.d/freetds.conf  </span>
</li>
<li class="alt"><span>ln -s /usr/local/webserver/freetds/lib/libsybdb.so.5.0.0 /usr/local/webserver/freetds/lib/libsybdb.so.4  </span></li>
<li><span>/sbin/ldconfig  </span></li>
<li class="alt"><span> </span></li>
<li><span>rm -f /usr/local/webserver/freetds/etc/freetds.conf  </span></li>
<li class="alt"><span>vi /usr/local/webserver/freetds/etc/freetds.conf  </span></li>
</ol>
Copy after login

输入以下内容:

引用

<ol class="dp-xml">
<li class="alt"><span><span>[global]  </span></span></li>
<li><span>        # TDS protocol version  </span></li>
<li class="alt">
<span>;       tds </span><span class="attribute">version</span><span> = </span><span class="attribute-value">4</span><span>.2  </span>
</li>
<li><span> </span></li>
<li class="alt"><span>        # Whether to write a TDSDUMP file for diagnostic purposes  </span></li>
<li><span>        # (setting this to /tmp is insecure on a multi-user system)  </span></li>
<li class="alt">
<span>;       dump </span><span class="attribute">file</span><span> = /tmp/freetds.log  </span>
</li>
<li>
<span>;       debug </span><span class="attribute">flags</span><span> = </span><span class="attribute-value">0xffff</span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li><span>        # Command and connection timeouts  </span></li>
<li class="alt">
<span>;       </span><span class="attribute">timeout</span><span> = </span><span class="attribute-value">10</span><span> </span>
</li>
<li>
<span>;       connect </span><span class="attribute">timeout</span><span> = </span><span class="attribute-value">10</span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li><span>        # If you get out-of-memory errors, it may mean that your client  </span></li>
<li class="alt"><span>        # is trying to allocate a huge buffer for a TEXT field.  </span></li>
<li><span>        # Try setting 'text size' to a more reasonable limit  </span></li>
<li class="alt">
<span>        text </span><span class="attribute">size</span><span> = </span><span class="attribute-value">64512</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>        </span><span class="attribute">host</span><span> = </span><span class="attribute-value">mssql</span><span>.yourdomain.com  </span>
</li>
<li>
<span>        </span><span class="attribute">port</span><span> = </span><span class="attribute-value">1433</span><span> </span>
</li>
<li class="alt">
<span>        tds </span><span class="attribute">version</span><span> = </span><span class="attribute-value">8</span><span>.0  </span>
</li>
<li>
<span>        client </span><span class="attribute">charset</span><span> = </span><span class="attribute-value">UTF</span><span>-8 </span>
</li>
</ol>
Copy after login

2、编译安装PHP自带MSSQL扩展

进入本地已存在的php-5.2.XX源码包目录:

<ol class="dp-xml">
<li class="alt"><span><span>cd /data0/software/php-5.2.XX/ext/mssql/  </span></span></li>
<li><span>/usr/local/webserver/php/bin/phpize  </span></li>
<li class="alt">
<span>./configure </span><span class="attribute">--with-php-config</span><span>=/usr/local/webserver/php/bin/php-config </span><span class="attribute">--with-mssql</span><span>=/usr/local/webserver/freetds/  </span>
</li>
<li><span>make && make install  </span></li>
</ol>
Copy after login

3、在php.ini配置文件中增加mssql.so

<ol class="dp-xml"><li class="alt"><span><span>vi /usr/local/webserver/php/etc/php.ini  </span></span></li></ol>
Copy after login

增加一行:

引用

<ol class="dp-xml"><li class="alt"><span><span class="attribute">extension</span><span> = </span><span class="attribute-value">"mssql.so"</span><span> </span></span></li></ol>
Copy after login

4、重启PHP FastCGI

<ol class="dp-xml"><li class="alt"><span><span>/usr/local/webserver/php/sbin/php-fpm restart  </span></span></li></ol>
Copy after login

5、测试文件(test_mssql.php):

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span>     </span></span></li>
<li>
<span>header("Content-type: text/html; </span><span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8");     </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">msdb</span><span>=</span><span class="attribute-value">mssql_connect</span><span>("mssql.yourdomain.com:1433","username","password");     </span>
</li>
<li><span>if (!$msdb) {     </span></li>
<li class="alt"><span>        echo "connect sqlserver error";     </span></li>
<li><span>        exit;     </span></li>
<li class="alt"><span>}     </span></li>
<li><span>mssql_select_db("database_name",$msdb);     </span></li>
<li class="alt">
<span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mssql_query</span><span>("SELECT top 5 * FROM table", $msdb);     </span>
</li>
<li>
<span>while($</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mssql_fetch_array</span><span>($result)) {     </span>
</li>
<li class="alt"><span>        var_dump($row);     </span></li>
<li><span>}     </span></li>
<li class="alt"><span>mssql_free_result($result);     </span></li>
<li>
<span class="tag">?></span><span>    </span>
</li>
</ol>
Copy after login


Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

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.

CentOS: What Led to the Decision to End Support CentOS: What Led to the Decision to End Support Apr 23, 2025 am 12:10 AM

RedHatendedsupportforCentOStoshifttowardsacommerciallyfocusedmodelwithCentOSStream.1)CentOStransitionedtoCentOSStreamforRHELdevelopment.2)ThisencourageduserstomovetoRHEL.3)AlternativeslikeAlmaLinux,RockyLinux,andOracleLinuxemergedasreplacements.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

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.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

How to understand DMA operations in C? How to understand DMA operations in C? Apr 28, 2025 pm 10:09 PM

DMA in C refers to DirectMemoryAccess, a direct memory access technology, allowing hardware devices to directly transmit data to memory without CPU intervention. 1) DMA operation is highly dependent on hardware devices and drivers, and the implementation method varies from system to system. 2) Direct access to memory may bring security risks, and the correctness and security of the code must be ensured. 3) DMA can improve performance, but improper use may lead to degradation of system performance. Through practice and learning, we can master the skills of using DMA and maximize its effectiveness in scenarios such as high-speed data transmission and real-time signal processing.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

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.

macOS vs. Linux: Exploring the Differences and Similarities macOS vs. Linux: Exploring the Differences and Similarities Apr 25, 2025 am 12:03 AM

macOSandLinuxbothofferuniquestrengths:macOSprovidesauser-friendlyexperiencewithexcellenthardwareintegration,whileLinuxexcelsinflexibilityandcommunitysupport.macOS,developedbyApple,isknownforitssleekinterfaceandecosystemintegration,whereasLinux,beingo

See all articles