【发布时间】:2019-06-19 06:52:22
【问题描述】:
我正在共享主机上开发一个 HTML、PHP 等网站。 我有一台具有 SQL Server 和 SSMS 2014 的本地计算机,其中包含我需要在该网站上获取的信息。 在我的共享主机上,我有这个 php 设置:
我已经通过 ip 地址连接到其他本地机器上的数据库,但是当我尝试在浏览器上连接时,我一遍又一遍地收到相同的错误。
我尝试使用 sqlsrv_connect:(出于安全原因,我没有显示 ip)
$serverName = "127.0.0.1";
$uid = "admin";
$pwd = "admin***";
$databaseName = "mydb";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
// Connect using SQL Server Authentication.
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
echo "code: ".$error[ 'code']."<br />";
echo "message: ".$error[ 'message']."<br />";
}
}
$tsql = "SELECT mor1_emp,mor2_emp,mor3_emp FROM rh_company";
sqlsrv_close( $conn);
我尝试使用 PDO odbc:
try {
$dsn = "mysql:dbname=testdb;host={$db_host1}";
$connection = new PDO("odbc:Driver={SQL Server};Server=127.0.0.1;Database=mydb; Uid=admin;Pwd=admin***;");
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
}
我尝试使用 PDO mssql:
try {
$hostname = "127.0.0.1"; //host
$dbname = "mydb"; //db name
$username = "admin"; // username like 'sa'
$pw = "admin***"; // password for the user
$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
}
$stmt = $dbh->prepare("SELECT * FROM table");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
我在使用 sqlsrv_connect 时总是遇到的错误:
SQLSTATE: IMSSP
code: -49
message: This extension requires the Microsoft ODBC Driver 11 or 13 for SQL
Server. Access the following URL to download the ODBC Driver 11 or 13 for SQL Server for x64: http://go.microsoft.com/fwlink/?LinkId=163712
SQLSTATE: IM002
code: 0
message: [unixODBC][Driver Manager]Data source name not found, and no default driver specified
使用 PDO(ODBC):
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
使用 PDO(mssql):找不到驱动程序
【问题讨论】:
标签: php sql-server odbc sqlsrv