Reputation: 121
I have searched through many posts trying to find an answer but get nowhere. I am trying to establish a connection to and Sql Server DB through my PHP web application using WAMP.
What I have tried:
I did not though that even though these are added in the php.ini they were not in the php> php extentions list - not sure why
This is my code below allow with the error
<?php
$serverName="DESKTOP-0KNJ0KP";
$connectionInfo=array("Database"=>"SPMS_db",);
$conn=sqlsrv_connect($serverName,$connectionInfo);
if ($conn) {
echo "Connected.<br />";
} else {
echo "Connection failed.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
I have added context fro PHPinfo()
Upvotes: 0
Views: 269
Reputation: 29943
You have installed PDO_sqlsrv
part of PHP Driver for SQL Server, but your code uses sqlsrv
functions. You have two options:
php_sqlsrv_
extensions to make these functions work orPDO
version of the driverPHP code using PDO version of PHP Driver for SQL Server:
<?php
# Connection
$server = "DESKTOP-0KNJ0KP";
$database = "SPMS_db";
try {
$conn = new PDO( "sqlsrv:Server=$server;Database=$database", NULL, NULL);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( "Error connecting to SQL Server. ".$e->getMessage() );
}
# End
$conn = null;
?>
Upvotes: 1
Reputation: 6975
Connecting through PDO
library (which is already installed in your server) is easier.
See PDO Book from PHP.NET: https://www.php.net/manual/en/ref.pdo-dblib.php
Upvotes: 0