如何使用PHP7连接到SQL Server? (我想念什么?) [英] How to connect to sql server using PHP7? (What am I missing?)
本文介绍了如何使用PHP7连接到SQL Server? (我想念什么?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是phpinfo的输出: version.php
这是代码:
$ serverName = XXXX;
$ connection = array( UID => UserID, PWD => Password123, Database => database_name);
$ conn = sqlsrv_connect($ serverName,$ connection);
if($ conn === false){
$ myfile3 = fopen( log.txt, w);
fwrite($ myfile3,sqlsrv_errors());
fclose($ myfile3);
};
$ tsql =从产品中选择前10个pName;
$ stmt = sqlsrv_query($ conn,$ tsql);
$ row = sqlsrv_fetch_array($ stmt);
$ myfile4 = fopen( log.txt, w);
fwrite($ myfile4,$ row [0]);
fclose($ myfile4);
sqlsrv_free_stmt($ stmt);
sqlsrv_close($ conn);
什么都不会写入日志文件。即使我在fwrite中硬编码文本($ myfile3,硬编码文本);
这是php.ini文件中的扩展部分
[ExtensionList]
; extension = php_mysql.dll
扩展名= php_mysqli.dll
扩展名= php_mbstring.dll
扩展名= php_gd2.dll
扩展名= php_gettext.dll
扩展名= php_curl.dll
扩展名= php_exif.dll
扩展名= php_xmlrpc.dll
扩展名= php_openssl.dll
扩展名= php_soap.dll
扩展名= php_pdo_mysql.dll
扩展名= php_pdo_sqlite.dll
扩展名= php_imap.dll
扩展名= php_tidy.dll
扩展名= php_sqlsrv_7_nts_x64.dll
; extension = php_sqlsrv_7_ts_x64.dll
最后,我知道我不需要所有这些,但是这些是我在ext文件夹中有4个dll。
php_sqlsrv_7_ts_x86.dll
解决方案
pdo:
$ serverName =(local)\sqlexpress;
/ *使用Windows身份验证进行连接。 * /
试试
{
$ conn = new PDO( sqlsrv:server = $ serverName; Database = AdventureWorks,,);
$ conn-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
}
catch(Exception $ e)
{
die(print_r($ e-> getMessage()));
}
程序方式:
$ serverName =(local)\sqlexpress;
$ connectionOptions = array( Database => AdventureWorks);
/ *使用Windows身份验证进行连接。 * /
$ conn = sqlsrv_connect($ serverName,$ connectionOptions);
if($ conn === false)
die(sqlsrv_errors());
单击此处以获取更多信息
Here's the phpinfo output: version.php
Here's the code:
$serverName = "X.X.X.X";
$connection = array( "UID"=>"UserID", "PWD"=>"Password123", "Database"=>"database_name");
$conn = sqlsrv_connect( $serverName, $connection);
if ($conn === false) {
$myfile3 = fopen("log.txt", "w");
fwrite($myfile3, sqlsrv_errors());
fclose($myfile3);
};
$tsql = "SELECT top 10 pName from products";
$stmt = sqlsrv_query( $conn, $tsql);
$row = sqlsrv_fetch_array($stmt);
$myfile4 = fopen("log.txt", "w");
fwrite($myfile4, $row[0]);
fclose($myfile4);
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
Nothing is written to the log file. Even if I hard code text in the fwrite($myfile3, "hard coded text"); place, nothing is written out.
Here's the extensions section in the php.ini file
[ExtensionList]
;extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_mbstring.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_curl.dll
extension=php_exif.dll
extension=php_xmlrpc.dll
extension=php_openssl.dll
extension=php_soap.dll
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_imap.dll
extension=php_tidy.dll
extension=php_sqlsrv_7_nts_x64.dll
;extension=php_sqlsrv_7_ts_x64.dll
Lastly, I know I don't need all of these, but these are 4 dlls I have in the ext folder.
php_sqlsrv_7_nts_x64.dll
php_sqlsrv_7_nts_x86.dll
php_sqlsrv_7_ts_x64.dll
php_sqlsrv_7_ts_x86.dll
解决方案
Using pdo:
$serverName = "(local)\sqlexpress";
/* Connect using Windows Authentication. */
try
{
$conn = new PDO( "sqlsrv:server=$serverName ; Database=AdventureWorks", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ) );
}
Procedural way:
$serverName = "(local)\sqlexpress";
$connectionOptions = array("Database"=>"AdventureWorks");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn === false )
die(sqlsrv_errors());
Click here for More Information
这篇关于如何使用PHP7连接到SQL Server? (我想念什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文