26
2025
06
12:29:44

幸福来得太突然: php-pdo-dblib 能连上 MSSQLSERVER 了, 还要什么 odbc ?

幸福来得太突然: php-pdo-dblib 能连上 MSSQLSERVER 了, 还要什么 odbc ?



PDO_DBLIB 是一个实现了 PHP 数据对象(PDO)接口的驱动,

可以通过 FreeTDS 库从 PHP 访问 Microsoft SQL Server 和 Sybase 数据库。

这个扩展在Windows上已经不可用了。

在Windows上,您才应该使用 SqlSrv,即

微软提供的MS SQL的替代驱动。

ref:
https://www.php.net/manual/en/ref.pdo-dblib.php

https://docs.microsoft.com/en-us/sql/connect/php/microsoft-php-driver-for-sql-server?redirectedfrom=MSDN&view=sql-server-ver16



如果不能使用SqlSrv,您还可以使用 PDO_ODBC 驱动来连接到 Microsoft SQL Server 和 Sybase 数据库,因为原生的 Windows DB-LIB 是古老的,线程不安全的,并且不再被微软支持。




ldconfig 命令

使用ldconfig命令来创建、更新和删除必要的链接和缓存(供运行时链接器ld.so使用),

以便在命令行上指定的目录、文件/etc/ld.so.conf和可信目录(/usr/lib、/lib64和/lib)中找到最新的共享库

ldconfig命令在确定哪些版本的链接应该被更新时,会检查它所遇到的库的头和文件名。

这个命令还创建了一个叫做/etc/ld.so.cache的文件,用来加快链接速度。




Array ( [0] => dblib [1] => firebird [2] => mysql [3] => odbc [4] => sqlite [5] => sqlsrv )

Success Results :

Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (Intel X86) Jun 28 2012 08:42:37 Copyright (c) Microsoft Corporation Express Edition on Windows NT 5.1(Build 2600: Service Pack 3) (Hypervisor)



<?php
print_r(PDO::getAvailableDrivers()); 

try {
    $serverName = "vb0xp";
    $databaseName = "ww_wchar";
    $uid = "sa";
    $pwd = "WTFWTF";
    
    $conn = new PDO("dblib:host=$serverName; dbname=$databaseName",$uid,$pwd);

    // Select Query
    $tsql = "SELECT @@Version AS SQL_VERSION";

    // Executes the query
    $stmt = $conn->query($tsql);
} catch (PDOException $exception1) {
    echo "<h1>wtf? Caught PDO exception:</h1>";
    echo $exception1->getMessage() . PHP_EOL;
    echo "<h1>PHP Info for troubleshooting</h1>";
    phpinfo();
}

?>

<h1> Success Results : </h1>

<?php
try {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo $row['SQL_VERSION'] . PHP_EOL;
    }
} catch (PDOException $exception2) {
    // Display errors
    echo "<h1> Ding! Caught PDO exception:</h1>";
    echo $exception2->getMessage() . PHP_EOL;
}

unset($stmt);
unset($conn);
?>



$ cat /etc/freetds.conf 
#
# 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 = 8.0

	# 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
	
	# To reduce data sent from server for BLOBs (like TEXT or
	# IMAGE) try setting 'text size' to a reasonable limit
;	text size = 64512

	# If you experience TLS handshake errors and are using openssl,
	# try adjusting the cipher list (don't surround in double or single quotes)
	# openssl ciphers = HIGH:!SSLv2:!aNULL:-DH

[vb0xp]
	host = vb0xp
	port = 1433
	tds version = 7.0    

[10.150.1.172]
	host = 10.150.1.172
	port = 1433
	tds version = 7.0

[mssql]
	host = 
	port = 1433
	tds version = 7.0


# A typical Sybase server
[egServer50]
	host = symachine.domain.com
	port = 1433
	tds version = 7.0

# A typical Microsoft server
[egServer73]
	host = ntmachine.domain.com
	port = 1433
	tds version = 7.0


[root@m4800linux ~]# dnf -y install php-pdo-dblib
Last metadata expiration check: 0:35:27 ago on Sun 28 Aug 2022 02:24:28 PM PDT.
Package php-pdo-dblib-8.1.9-1.fc36.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@m4800linux ~]# dnf whatprovides php-pdo-dblib
Last metadata expiration check: 0:35:53 ago on Sun 28 Aug 2022 02:24:28 PM PDT.
php-pdo-dblib-8.1.4-1.fc36.x86_64 : PDO driver for Microsoft SQL Server and Sybase databases
Repo        : fedora
Matched from:
Provide    : php-pdo-dblib = 8.1.4-1.fc36

php-pdo-dblib-8.1.9-1.fc36.x86_64 : PDO driver for Microsoft SQL Server and Sybase databases
Repo        : @System
Matched from:
Provide    : php-pdo-dblib = 8.1.9-1.fc36

php-pdo-dblib-8.1.9-1.fc36.x86_64 : PDO driver for Microsoft SQL Server and Sybase databases
Repo        : updates
Matched from:
Provide    : php-pdo-dblib = 8.1.9-1.fc36


/etc/hosts

10.150.1.172  vb0xp




REF:

https://stackoverflow.com/questions/20163776/connect-php-to-mssql-via-pdo-odbc
https://stackoverflow.com/questions/37205752/sqlstate01002-adaptive-server-connection-failed-severity-9

https://stackoverflow.com/questions/37205752/sqlstate01002-adaptive-server-connection-failed-severity-9







Array ( [0] => dblib [1] => mysql [2] => odbc [3] => sqlite [4] => sqlsrv )

Success Results :

transactional -- 90 -- 0 -- 0 merge -- 90 -- 0 -- 0 security_model -- 90 -- 0 -- 0



<?php
print_r(PDO::getAvailableDrivers()); 

try {
    $serverName = "vm64win10dad";
    $databaseName = "master";
    $uid = "sa";
    $pwd = "WTFWTF";
     $conn=new PDO("dblib:host=$serverName;dbname=$databaseName",$uid,$pwd);
    // SELECT @@Version AS SQL_VERSION;
    //$tsql = "SELECT @@Version AS SQL_VERSION;" ;
    $tsql = " SELECT TOP 1000 [optname],[value],[major_version],[minor_version],[revision],[install_failures]FROM [master].[dbo].[MSreplication_options];" ;
    // Executes the query
    $stmt = $conn->query($tsql);
    } catch (PDOException $exception1) {
    echo "<h1> YUCK! Caught PDO exception:</h1>";
    echo $exception1->getMessage() . PHP_EOL;
    echo "<h1>PHP Info for troublesho000000000000000000000000oting ----------->> </h1>";    phpinfo();
    }
?>

<h1> Success Results : </h1>

<?php
try {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo $row['optname'] . " -- " . $row['major_version'] . " -- " . $row['minor_version'] . " -- " . $row['install_failures'] .PHP_EOL  ;
    }
} catch (PDOException $exception2) {
    // Display errors
    echo "<h1>Caught PDO exception:</h1>";
    echo $exception2->getMessage().PHP_EOL;
}

unset($stmt);
unset($conn);
?>






# dnf -y install php-pdo-dblib
Last metadata expiration check: 3:10:25 ago on Sun 28 Aug 2022 12:27:17 PM PDT.
Dependencies resolved.
==================================================================================================================================
 Package                           Architecture               Version                           Repository                   Size
==================================================================================================================================
Installing:
 php-pdo-dblib                     x86_64                     8.1.9-1.fc36                      updates                      32 k

Transaction Summary
==================================================================================================================================
Install  1 Package

Total download size: 32 k
Installed size: 74 k
Downloading Packages:
php-pdo-dblib-8.1.9-1.fc36.x86_64.rpm                                                              84 kB/s |  32 kB     00:00    
----------------------------------------------------------------------------------------------------------------------------------
Total                                                                                              32 kB/s |  32 kB     00:00     
......                                                                  1/1 

Installed:
  php-pdo-dblib-8.1.9-1.fc36.x86_64                                                                                               

Complete!






Array ( [0] => dblib [1] => mysql [2] => odbc [3] => sqlite [4] => sqlsrv )

Success Results :

Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (Intel X86) Jun 28 2012 08:42:37 Copyright (c) Microsoft Corporation Express Edition on Windows NT 5.1(Build 2600: Service Pack 3)

-------------------------------------------------------------------------------------------------->>

transactional -- 90 -- 0 -- 0

------------------------------------------------------------------------------------------------<<--


<?php
print_r(PDO::getAvailableDrivers()); 

try {
    $serverName = "xpvbxvmdad";
    $databaseName = "master";
    $uid = "sa";
    $pwd = "wtfwtfwtf";
    $conn=new PDO("dblib:host=$serverName;dbname=$databaseName",$uid,$pwd);
    $tsql = " SELECT @@Version AS SQL_VERSION, [optname],[value],[major_version],[minor_version],[revision],[install_failures]FROM [master].[dbo].[MSreplication_options];" ;
    // Executes the query
    $stmt = $conn->query($tsql);
    } catch (PDOException $exception1) {
    echo "<h1> YUCK! Caught PDO exception:</h1>";
    echo $exception1->getMessage() . PHP_EOL;
    echo "<h1>PHP Info for troublesho000000000000000000000000oting ----------->> </h1>";
    phpinfo();
    }
?>

<h1> Success Results : </h1>

<?php
try {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo $row['SQL_VERSION'] . PHP_EOL  ;
        echo "<h1> -------------------------------------------------------------------------------------------------->> </h1>";
        echo $row['optname'] . " -- " . $row['major_version'] . " -- " . $row['minor_version'] . " -- " . $row['install_failures'] .PHP_EOL  ;
        echo "<h1> ------------------------------------------------------------------------------------------------<<-- </h1>";
    }
} catch (PDOException $exception2) {
    // Display errors
    echo "<h1>Caught PDO exception:</h1>";
    echo $exception2->getMessage().PHP_EOL;
}

unset($stmt);
unset($conn);
?>





ref

https://www.w3schools.com/php/php_mysql_select.asp




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/11940.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: