Linux平臺使用Freetds連接SQL Server服務(wù)器,兼容PHP和Laravel
May 01, 2017 am 10:40 AM本文介紹Linux平臺如何使用Freetds連接SQL Server服務(wù)器,兼容PHP和Laravel,希望對php中文網(wǎng)php初學(xué)者有所幫助!
本文在CentOS 7 64bit和Laravel 4.2環(huán)境測試通過。
1.下載源碼并解壓縮
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz tar zxvf freetds-stable.tgz cd freetds-0.91
2.配置并生成makefile
./configure --with-tdsver=8.0 --enable-msdblib
3.編譯安裝
make sudo make install
4.配置
默認安裝的配置文件位于/usr/local/etc,在/usr/local/etc編輯freetds.conf 文件,默認為
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $ # # This file is installed by FreeTDS if no file by the same # name is found in the installation directory. # # For information about the layout of this file and its settings, # see the freetds.conf manpage "man freetds.conf". # Global settings are overridden by those in a database # server specific section [global] # TDS protocol version ; tds version = 4.2 # Whether to write a TDSDUMP file for diagnostic purposes # (setting this to /tmp is insecure on a multi-user system) ; dump file = /tmp/freetds.log ; debug flags = 0xffff # Command and connection timeouts ; timeout = 10 ; connect timeout = 10 # If you get out-of-memory errors, it may mean that your client # is trying to allocate a huge buffer for a TEXT field. # Try setting 'text size' to a more reasonable limit text size = 64512 # A typical Sybase server [egServer50] host = symachine.domain.com port = 5000 tds version = 5.0 # A typical Microsoft server [egServer70] host = ntmachine.domain.com port = 1433 tds version = 7.0
在文件的最后位置添加如下配置,即可連接SQL Server 2000
[sql-server-2000] host = 192.168.182.9 port = 1433 tds version = 7.0
如果要連接SQL Server 2005或2008,需要添加以下配置
[sql-server-2005] host = 192.168.70.1 port = 1433 tds version = 8.0
4.測試
/usr/local/bin/tsql -S sql-server-2000 -U sa -P test
如果成功連接,將會出現(xiàn)以下提示
locale is "zh_CN.UTF-8" locale charset is "UTF-8" using default charset "UTF-8" 1>
至此,F(xiàn)reeTDS已經(jīng)是Linux具備連接SQL Server的功能了。
5.編譯PHP擴展
PHP 5.4之后已經(jīng)沒有原生支持的SQL Server的驅(qū)動了,因此需要手動編譯PHP源碼的擴展添加對SQL Server的驅(qū)動支持。CentOS 7自帶的是5.4版本的PHP,因此我們通過編譯5.4版的PHP源碼獲得擴展。
目前CentOS yum源里最新的php是5.4.16,php可以通過yum安裝到系統(tǒng)
sudo yum install php php-devel php-fpm php-common php-mysql php-pdo libzip
php官網(wǎng)上最新的5.4版本是 5.4.39,下載源碼到本地
wget http://cn2.php.net/distributions/php-5.4.39.tar.gz
解壓并進入擴展目錄
tar zxvf php-5.4.39.tar.gz cd php-5.4.39/ext/mssql
使用phpize生成configure腳本文件
phpize
生成makefile
./configure
編譯
make
編譯之后將會在modules子目錄生成mssql.so擴展文件。復(fù)制擴展文件到php的擴展文件目錄
sudo cp modules/mssql.so /usr/lib64/php/modules/
在/etc/php.d目錄下新建mssql.ini 文件,輸入以下內(nèi)容
; Enable mssql extension module extension=mssql.so
這樣PHP就能加載SQL Server驅(qū)動了。使用如下代碼測試PHP連接SQL Server。
<?php header("Content-type: text/html; charset=utf-8"); $msdb=mssql_connect("sql-server-2000","sa","test"); if (!$msdb) { echo "connect sqlserver error"; exit; } mssql_select_db("msdb",$msdb); $result = mssql_query("SELECT top 5 * FROM employee", $msdb); while($row = mssql_fetch_array($result)) { var_dump($row); } mssql_free_result($result); ?>
代碼中的數(shù)據(jù)庫配置信息可以替換成別的。測試命令如下
php -f test-mssql.php
成功執(zhí)行后將會打印出數(shù)據(jù)庫表中記錄數(shù)據(jù)。
目前原生PHP代碼已經(jīng)可以連接SQL Server了,但是Laravel還是不行,還需要再編譯生成一個pdo_dblib.so擴展驅(qū)動。
6.編譯pdo_dblib.so擴展適配Laravel
cd php-5.4.39/ext/pdo_dblib ./configure make sudo cp modules/pdo_dblib.so /usr/lib64/php/modules/
再到/etc/php.d下新建pdo_dblib.ini,輸入以下內(nèi)容
; Enable pdo_dblib extension module extension=pdo_dblib.so
再編輯Laravel的app/config/database.php文件,將sqlsrv區(qū)域改為一下形式
'sqlsrv' => array( 'driver' => 'sqlsrv', 'host' => 'sql-server-2000', 'database' => 'msdb', 'username' => 'sa', 'password' => 'test', 'prefix' => '', ),
這樣Laravel也可以連接SQL Server了。

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

As a pioneer in the digital world, Bitcoin’s unique code name and underlying technology have always been the focus of people’s attention. Its standard code is BTC, also known as XBT on certain platforms that meet international standards. From a technical point of view, Bitcoin is not a single code style, but a huge and sophisticated open source software project. Its core code is mainly written in C and incorporates cryptography, distributed systems and economics principles, so that anyone can view, review and contribute its code.

Linuxcanrunonmodesthardwarewithspecificminimumrequirements.A1GHzprocessor(x86orx86_64)isneeded,withadual-coreCPUrecommended.RAMshouldbeatleast512MBforcommand-lineuseor2GBfordesktopenvironments.Diskspacerequiresaminimumof5–10GB,though25GBisbetterforad

To enable PHP containers to support automatic construction, the core lies in configuring the continuous integration (CI) process. 1. Use Dockerfile to define the PHP environment, including basic image, extension installation, dependency management and permission settings; 2. Configure CI/CD tools such as GitLabCI, and define the build, test and deployment stages through the .gitlab-ci.yml file to achieve automatic construction, testing and deployment; 3. Integrate test frameworks such as PHPUnit to ensure that tests are automatically run after code changes; 4. Use automated deployment strategies such as Kubernetes to define deployment configuration through the deployment.yaml file; 5. Optimize Dockerfile and adopt multi-stage construction

Building an independent PHP task container environment can be implemented through Docker. The specific steps are as follows: 1. Install Docker and DockerCompose as the basis; 2. Create an independent directory to store Dockerfile and crontab files; 3. Write Dockerfile to define the PHPCLI environment and install cron and necessary extensions; 4. Write a crontab file to define timing tasks; 5. Write a docker-compose.yml mount script directory and configure environment variables; 6. Start the container and verify the log. Compared with performing timing tasks in web containers, independent containers have the advantages of resource isolation, pure environment, strong stability, and easy expansion. To ensure logging and error capture

Add useradd or adduser commands commonly used by users in Linux. 1. When using useradd, you need to manually set the password and home directory. Add the -m parameter to create the home directory; 2. You can specify the shell, group and UID through parameters such as -s, -G, and -u; 3. Adduser is an interactive command, suitable for novices to automatically complete the configuration; 4. Pay attention to permissions, username uniqueness and home directory permissions; 5. Userdel can be used to delete users and home directory by mistake. Mastering these key points allows you to manage users efficiently and securely.

When designing a relational database, four key principles should be followed. First, correctly use primary and foreign key constraints to ensure data integrity and association accuracy; second, perform standardized design reasonably, usually reaching the third normal form (3NF), eliminating redundancy and ensuring data consistency; third, establishing appropriate indexes for common queries to improve query performance but avoid over-index; finally, using consistent naming specifications and structural styles to enhance readability and maintainability. Mastering these principles can help build a clear, efficient and robust database structure.

SQL's WHERE clause is used to accurately filter rows of data that meet the criteria. 1. Use comparison operators (such as =, >, 30; 2. Use AND and OR to combine multiple conditions to improve query flexibility, such as SELECTFROMusersWHEREage>30ANDcity='Beijing'; 3. Use LIKE, IN, BETWEEN to implement fuzzy matching and range query, such as SELECT*FROMusersWHEREnameLIKE'J%'; 4. Pay attention to case sensitivity and processing of NULL values, such as using ISNULL or ISNOTNULL to judge NULL values to ensure that the query results are accurate.

Table name change is usually implemented in SQL using the RENAMETABLE or ALTERTABLE command. 1.MySQL, MariaDB and other databases use RENAMETABLEold_table_nameTOnew_table_name; syntax, supports batch operations; 2. SQLServer requires sp_rename stored procedure, and the syntax is EXECsp_rename'old_table_name','new_table_name'; 3.PostgreSQL uses ALTERTABLEold_table_nameRENAMETOnew_table_name
