I have connected php with sql server when both these lie on my local computer.
But I need to connect with an SQL Server Web Database when my PHP script is running on local computer.
I have added the extensions php_pdo_sqlsrv_54_ts.dll
and php_sqlsrv_54_ts.dll
in my PHP extensions.
For connection with SQL Server on my localhost I also needed a SQL Server Native Client Driver which I have already installed but does this also have to do anything with my connection to SQL Server web database?
When I run the PHP Script the script takes some time to execute and sometimes gives an error :
SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]TCP Provider: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. 1
My connection script is
$serverName = "***.db.*****.********.com";
try{
$conn = new PDO("sqlsrv:server=$serverName;Database=*****", "***UserName***", "***PWD***");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e){
die( print_r( $e->getMessage() ) );
}
$conn = new PDO("sqlsrv:server=$serverName;Database= * ", " DBNAME ", " PWD "); Instead of DB name , here username should come
ping serverip
Example ping 192.168.100.12
Try this too ..
try {
$hostname = "host"; //host
$dbname = "dbname"; //db name
$username = "user"; // username like 'sa'
$pw = "pass"; // 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";
exit;
}
$stmt = $dbh->prepare("SELECT * FROM table");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
You can find more examples over here http://php.net/manual/en/ref.pdo-dblib.php
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.