PHP access a database in MS SQL server 2008 r2 - php

Dears,
I have a php application and I cannot access the SQL server database even though I installed the drivers and modify the php.ini file.
when I write the following code it doesn't provide me with an output
$servername = "localhost";
$info = array('Databse'=>'Test');
$con = sqlsrv_connect($servername);
if($con)
{
echo "Success";
}
else
{
echo "Failure";
}
anything below the sqlsrv_connect() is ignored.
Please help me I've been stuck in this for the past 3 weeks :(

You could print out the connection error with using sqlsrv_errors()
$con=sqlsrv_connect($servername);
if($con)
{
echo "Success";
}
else
{
die( print_r( sqlsrv_errors(), true));
echo "Faluire";
}
For further info check manual http://php.net/manual/en/function.sqlsrv-connect.php

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.

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

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

not connecting to the database

To connect mssql from my android application, I wrote the following script:
<?php
$myServer= "*****";
$myUser="*****";
$myPass="***";
$db="*****";
echo "hi";
$dbhandle = mssql_connect($myServer, $myUser, $myPass);
echo $dbhandle;
if($dbhandle) {
echo "success";
} else {
echo "failed";
}
$database = mssql_select_db($db);
?>
When I test it in a browser, it's showing only "hi" and it's not executing remaining lines of code. What would be the problem here?
You have not enabled the mssql module in PHP. If you find the error logs, it would say something like "mssql_connect is not a function".
The mssql support built into PHP has been broken for some time. I would suggest following the instructions on this page to help you get it configured.

Categories