简体   繁体   English

PHP-PDO错误:在DSN字符串中指定了无效的关键字“host”

[英]PHP-PDO error: An invalid keyword 'host' was specified in the DSN string

Trying to connect to mssql server on smarterasp.net dns but without success so far. 尝试连接到smarterasp.net上的mssql服务器dns但到目前为止没有成功。 Had a prior issue with the drivers. 先前有过司机问题。 Installed them and they are working perfectly (added them in php.ini) 安装它们并且它们完美地工作(在php.ini中添加它们)

Using php version:5.6 , added to php.ini the following in order for the php-pdo-mssql to work: 使用php版本:5.6 ,为了让php-pdo-mssql正常工作,将以下内容添加到php.ini中:

  • extension=php_pdo_sqlsrv_56_nts.dll 延长= php_pdo_sqlsrv_56_nts.dll
  • extension=php_pdo_sqlsrv_56_ts.dll 延长= php_pdo_sqlsrv_56_ts.dll
  • extension=php_sqlsrv_56_nts.dll 延长= php_sqlsrv_56_nts.dll
  • extension=php_sqlsrv_56_ts.dll 延长= php_sqlsrv_56_ts.dll

What have I done so far on this code: 到目前为止,我对此代码做了什么:

<?php
 try {
     $conn = new PDO("sqlsrv:host=host_name_string;dbname=database_name_string", "username_string", "password_string");

     // set the PDO error mode to exception
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     echo "Connected successfully"; 
 }
 catch(PDOException $e)
 {
     echo "Connection failed: " . $e->getMessage();
 }

?>
  • instead of dbname I have put database, it doesn't work 而不是dbname我已经把数据库,它不起作用
  • instead of dbname I have put Database, it doesn't work 而不是dbname我已经把数据库,它不起作用
  • I have added the port 1433 after the host name example: 我在主机名示例后添加了端口1433:

"sqlsrv:host=host_name_string,1433;Database=database_name_string" “SQLSRV:主机= host_name_string,1433;数据库= database_name_string”

it doesn't work 它不起作用

  • I have added the port 1433 after the host name example in this way 我以这种方式在主机名示例之后添加了端口1433

"sqlsrv:host=host_name_string;port=1433;Database=database_name_string" “SQLSRV:主机= host_name_string;端口= 1433;数据库= database_name_string”

it doesn't work 它不起作用

I have tested the connection with my SQL sever management studio and the given data (values that I'm using here as parameters are 100% fine) 我已经测试了与我的SQL服务器管理工​​作室和给定数据的连接(我在这里使用的值作为参数是100%罚款)

What are my other options? 我还有什么其他选择?

the correct format for the connection is: 正确的连接格式是:

$conn = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password"); 

from the manual page: PDO_SQLSRV DSN 从手册页: PDO_SQLSRV DSN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM