MSSQL DATABASE CONNECTION WITH PHP - php

Please i am trying to connect to an mssql database on a different machine. Below is my code but i just keep getting a blank page.
I dont know what the issue may be. i have installed php and the mssql drivers.
<?php
$uid = "******";
$pwd = "***\$\$****";
$DB = "***********";
$serverName = "192.***.**.***";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=> $DB, "ReturnDatesAsStrings" => true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>

I would suggest that you display the connection error using sqlsrv_errors().
Load the PHP drivers in php.ini file and restart the server.
extension=php_pdo_sqlsrv_54_ts.dll
extension=php_sqlsrv_54_ts.dll
Please see the following example:
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
For more info: http://php.net/manual/en/function.sqlsrv-connect.php

This is for Server 2019 and PHP 7.4.
Download SQLSRV 5.8.0 Drivers here
Have this in your php.ini: (depending on hardware 32 vs 64)
extension=pdo_sqlsrv_74_ts_x64
extension=sqlsrv_74_ts_x64
Use this to create a mssql connection:
Function connect_db() {
$this->connectionInfo = Array("UID" => DB_USER, "PWD" => DB_PASSWORD, "Database" => DB_NAME);
$this->conn = SQLSRV_CONNECT(DB_SERVER, $this->connectionInfo);
if ($this->conn){ return; }
else {echo "Error";die( print_r( sqlsrv_errors(), true ));}
}

Related

How can I connect to SQL Server in Drupal 7?

I need to show some fields from another database (SQL Server), in my form in Drupal 7, but don't need disconnect from MySql.
I'm creating a custom module in Drupal 7 and in this module create connect to SQL Server, but this doesn't work, can anyone help me?
<?php
$serverName = "test"; //serverName\instanceName
$connectionInfo = array( "Database"=>"testdb", "UID"=>"user", "PWD"=>"qwer");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>

What are requirements for php and sql server connection?

I can connect Mssql Server using SQL Management Studio. But I need to connect to Mssql server from PHP, Apache Server. What should I do?
I just try this codes.
<?php
$serverName = "192.168.2.100\\sqlexpress"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"RGD_MAKNA_2019T", "UID"=>"RGD", "PWD"=>"123RGD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if($conn) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
}
?>
it gives me this
errror:"sqlsrv_connect is not function"

PHP Connect to MS SQL Database

I have a website locally hosted on my computer. My server is using PHP 5.6.21. I am trying to connect to a SQL Database on another computer Microsoft SQL Server 2000.
I have installed the PHP extensions, PHP_sqlsrv_56_ts and PHP_sqlsrv_56_nts. I have enabled them in the php.ini:
extension=php_sqlsrv_56_nts.dll
extension=php_sqlsrv_56_ts.dll
I have tried to connect using this:
<?php
$serverName = "serverName\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
And I tried using this:
<?php
$connection_string = 'DRIVER={SQL Server};SERVER=<Hector\SQLEXPRESS>;DATABASE=tempdb';
$user = 'sqltestclient';
$pass = 'paSSword';
$connection = odbc_connect( $connection_string, $user, $pass ) or die("Unable to connect to server");
echo $connection.' '.$user.' '.$pass;
?>
But neither work, the top wont load the page and the bottom just sasys unable to connect.
Im not sure what Im doing wrong
I was able to solve my problem using the information from this link.
http://www.techrepublic.com/article/access-microsoft-sql-server-2000-using-php/
May be this will help:
$serverName = "SERVER\\NAME";
$connInfo = array("Database"=>"stockdata");
$conn = sqlsrv_connect($serverName, $connInfo);
if($conn){
$sql = "SELECT * FROM users";
$res = sqlsrv_query($conn,$sql);
$arr = sqlsrv_fetch_array($res,SQLSRV_FETCH_ASSOC);
print_r($arr);
}else{
echo "Connection could not be established.<br />";
die( print_r(sqlsrv_errors(), true));
}
If you are using Windows Authentication no need to provide credentials ,otherwise you will get exception

php :Fatal error: Call to undefined function sqlsrv_connect()

I Have a big problem And I Wish get Solved for this problem here
I Try to Connect to Sql Server 2008 R2 using:
php 5.6.11
Xampp 5.6.3
Sql Server 2008 r2
Using sql server drivers for PHP (php_pdo_sqlsrv_56_ts.dl &
php_sqlsrv_56_ts.dll)
and my code to try connection is :
$serverName = "Mahmoud-HP\SQL2008R2"; //serverName\instanceName
$connectionInfo = array( "Database"=>"HR16", "UID"=>"Mahmoud", "PWD"=>"123" , "MultipleActiveResultSets" => false);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
but when run the page this message come to me
Fatal error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\testsqlserver\index.php on
What is solution for this problem
you have to use the SQL Server native driver for php.
Download from here:
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=20098
Example Code:
$serverName = "tcp:ServerID.database.windows.net, 1433";
$connectionOptions = array("Database" => "DatabaseName",
"UID" => "Username#ServerID",
"PWD" => "password");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
EDIT
Make sure you load BOTH dll's listed:
extension=php_sqlsrv_56_ts.dll
and
extension=php_pdo_sqlsrv_56_ts.dll

PHP program to connect to SQL Server

I've been doing a project in PHP(xampp) which requires to connect to a SQL Server database.
I've already done things like downloading and installing SQLSRV30.exe in C:\xampp\php\ext folder and creating a simple program which determine whether the program is already connected or not but still couldn't figure it out the problem that i encountered.
$server = "IDEA-PC\SQLEXPRESS";
$dbGet = array("Database"=>"LogboxDB");
$con = sqlsrv_connect($server, $dbGet) or die (sqlsrv_error());
if(!$con)
{
die(print_r(sqlsrv_errors(), true));
}
else
{
echo 'Connected';
}
Here is my error.
Fatal error: Call to undefined function sqlsrv_connect();
How can I get rid of this problem?
You need to enable the extension support to use the MSSQL server in PHP. It can be enabled by editing the php.ini as follow.
extension=php_sqlsrv.dll
You can donwload the package from http://docs.gurock.com/testrail-admin/howto-installing-sqlsrv
Try This
$serverName = "192.168.1.223, 1433"; //serverName\instanceName - change it as yours
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"LogboxDB", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}

Categories