我正在用HTML,PHP等在共享主机上开发一个网站。 我有一台装有SQL Server和SSMS 2014的本地计算机,其中包含我需要在该网站上获取的信息。 在我的共享主机上,我有以下php设置:
我已经通过IP地址连接到其他本地计算机上的db,但是当我尝试在浏览器上连接时,我一遍又一遍地得到相同的错误。
我尝试了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):找不到驱动程序
答案 0 :(得分:0)
使用PDO时,正确的语法是:
$dbh = new PDO ("**sqlsrv**:Server=$hostname,$port;Database=$dbname","$username","$pw");