How can I connect to SQL Server in Drupal 7? - php

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));
}
?>

Related

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"

Tring to connect Microsoft Sql server with PHP in MAMP

Im trying to use MS SQL server with PHP in MAMP and I get an error 500.
Below is the php code that Im using.
I learned today that I have to instal a driver but I don't know how to do it in MAMP.:
https://learn.microsoft.com/en-us/iis/application-frameworks/install-and-configure-php-on-iis/install-the-sql-server-driver-for-php
Any idea? many thanks,
<?php
$serverName = "MPR01\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"=>"Fund_Lib");
$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));
}
?>
With the error message in hand I just found the answer in this post:
https://es.stackoverflow.com/questions/55679/fatal-error-call-to-undefined-function-sqlsrv-connect

MSSQL DATABASE CONNECTION WITH 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 ));}
}

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

MSSQL server not working with php-Error shown

I am currently using PHP 5.2, with Microsoft Server 2008 R2, and using sqlsrv, with SQL native client 10.0.
I have changed the php extension to include extension=php_sqlsrv_52_ts_vc6.dll and have done the necessary changes to php.ini.
The php.info page also shows the sqlsrv has been installed successfully, but I am not sure why when I try to use sqlsrv command, it is unable to connect to SQL server. This is my codes.
<?php
$myServer = "MYSERVER\SQLEXPRESS";
$myUser = "sa";
$myDB = "Form";
$conInfo = array('Database'=>$myDB, 'UID'=>$myUser);
$conn = sqlsrv_connect( $myServer, $conInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
It shows [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'sa'. [message]. Could someone be kind enough to tell me what seems to go wrong?
I need the answer urgently.Thank you.
Try with this -
<?php
$myServer = "MYSERVER\SQLEXPRESS";
$myUser = "sa";
$myDB = "Form";
$conInfo = array('Database'=>$myDB, 'UID'=>$myUser, 'PWD'=>'');
$conn = sqlsrv_connect( $myServer, $conInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>

Categories