How to connect Microsoft SQL Database using PHP - php

i am trying to connect mssql database using sqlsrv_connect() php function but it's always show below fatal error.
Fatal error: Call to undefined function sqlsrv_connect().
Code:
$server = 'xx.xxx.xxx.Xxx\sqlexpress';
$username = 'uname';
$password = 'password';
$database = 'testdb';
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}

For this you have to download the associated driver.
The Microsoft SQL Server 2005 Driver for PHP allows PHP developers to access data in SQL Server 2005 and SQL Server 2008 databases. The driver includes support for Windows and SQL Server Authentication methods, transactions, parameter binding, streaming, metadata access, connection pooling, and error handling.
Check these links:
How to connect to MSSQL
Php Help Documnetation

Related

Connecting PHP application to MS Sql Server on prem

I am developing an application to upload data throught PHP to a SQL DB stored in a MS Sql Server on prem. The main problem I am having right now, is that i am not able to connect my application to the server, using sqlsrv_connect, and everytime I run a query with that application I get 502 bad gateway. Is there something i need to install to php or to the sql server to make the application work?
Thank you for your help
p.s. I inserted the code snippet below
<?php
$serverName = "";
$connectionInfo = array( "Database"=>"", "UID"=>"", "PWD"=>"");
$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));
}
?>
First install the Microsoft Drivers for PHP for SQL Server and then check the Getting started section which gives you info about how to connect.

PHP script to connect to SQL Server

I am running this script
<?php
$mysqli = mysqli_connect("ServerName", "username", "password", "dbname");
$res = mysqli_query($mysqli, "SELECT * from table1");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
?>
I am getting this error:
mysqli_connect(): (HY000/2002): No connection could be made because
the target machine actively refused it
I have looked around online, but I can't figure out what is wrong. All the credentials are correct and it still doesn't work.
I am connecting to a local db on SQL Server through SQL Server authentication. Do I need MySQL running on xamp for this to work? Any ideas?
I am using Windows 10 through bootcamp on a mac. This is for SQL Server, not for mysql
It seems that you want to connect to a Microsoft SQL Server database. In this case maybe this code helps:
<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$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));
}
?>
Also, if you run your php server locally with XAMPP you need to have Apache activated. If you try to connect to a MS SQL Server database you dont have to have MySQL enabled since you are not trying to connect to a MySQL database.
Source

Connect to MS SQL Server with PHP without drivers

A project at work has come up that requires me to connect to a Microsoft SQL Server 2014 in PHP. I have found tons of solutions online that involve downloading a set of drivers to either use PDO, sqlsrv, or ODBC.
After contacting my hosting provider this is what they said:
As you're on shared hosting, drivers can't be installed as it's not
supported on our platform.
You can use normal ODBC to connect if required
I so far haven't been able to find a solution that works without having to install any drivers/extensions. The hosting provider recommended ODBC but I can't seem to get my code to work for ODBC either. Does anyone know any other way I would be able to do this?
The code I'm currently using (which gives me an error:
Can't open lib 'SQL Server'
$server = 'xxx.xxx.xxx.xxx';
$user = 'user.....';
$pass = 'password...';
$port='1433';
$database = 'database...';
$connection_string = "DRIVER={SQL Server};SERVER=$server;PORT=$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
echo "Connection established.";
} else{
die("Connection could not be established.");
}
Have you tried 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));
}
?>
Refer: https://www.php.net/manual/en/function.sqlsrv-connect.php

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

Connection Error PHP with SQL server 2012

I'm facing an issue in connection PHP with SQL server 2012. Configured and (.dll) files and configured them.
In SQL server management studio i create a database named it "helloworld". My SQL server authentication mode is window.
The issue is connection established on system database like "master,msdb" but the database i created. It says:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "helloworld" requested by the login. The login failed.
The code I'm using is:
$serverName = "(local)"; //serverName\instanceName
$connectionInfo = array( "Database"=>"master","ReturnDatesAsStrings" => false,"CharacterSet" => 'utf-8');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
}
Any help will be appreciated. I just need to make a connection with the database i created not the system database.
Thanks
$con=mysql_connect('localhost','user','pass') or DIE ('Not Connected');
mysql_select_db("example") or die('Not Connected');
echo "successful" ;

Categories