I want to connect to a MSSQL 2008 database using PHP. Below is the code I am trying to use
<?php
$serverName = "psherathpc"; //serverName\instance
$connectionInfo = array("Database">"shoe_shop", "UID"=>"psherathpc\psherath", "PWD"=>"" );
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn)
{
echo "Connection established.<br/>";
}
else
{
echo "Connection could not be established.<br/>";
die( print_r( sqlsvr_error(), true));
}
?>
I am receiving the error Fatal error sqlsrv_connect(). How can I solve this problem? Please tell me step-by-step clearly.
You should always state exactly what error you are receiving but..
$conn = sqlsrv_connect($severName, $connectionInfo);
you mispelled $serverName. This possibly could be one solution.
Related
I have loaded the SQL drivers in php/etc folder and also have edited path in php.ini. I can see sqlsrv in php info as well.
This is the code that I am using to connect to SQL Server on another server.
I tried server name "10.10.10.38:1433" this also nothing worked for me.
$serverName = "10.10.10.38,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));
}
But I am getting following error:
Please help to resolve the issue.
Have you enabled pdo_sqlsrv ?
Have you installed ODBC drivers?
https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15
After installing the right ODBC drivers as Maxi Malvido told you you can use a PDO connection:
<?php
$conn = new PDO('sqlsrv:Server=yourserver; Database=test', 'login', 'password');
if ($conn)
{
echo "Connection established";
}
else {
echo "Connection not established";
}
?>
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
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
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));
}
First of all: I did a research on google and stackoverflow, but it does not helped me.
I'm a beginner, so please do not blame me directly by just reading the title and voting me down. Stackoverflow seems to be very aggressive sometimes :(
I'm trying to execute a simple stored procedure from PHP.
Connecting to the database works.
This code also works through a query in SQL Server Management Studio:
Execute SP_TPL_DeleteUser
#ExternalFieldID = 22
Regarding to this manuals: PHP Stored Procedures and SQL Server and MSSQL Bind
I build this lines of code:
$id=22;
$stmt=mssql_init("SP_TPL_DeleteUser", $conn);
mssql_bind($stmt, "#ExternalFieldID", $ExternalFieldID, SQLVARCHAR, false, false, 255);
mssql_execute($stmt);
mssql_free_statement($stmt);
My output is:
( ! ) Fatal error: Call to undefined function mssql_init() in
C:\Users\kians_000\dev\traka\index.php on line 32
Of course I tried playing around, but nothing works :(
I would be thankful for any tips.
Edit:
I can connect to the DB with this:
$serverName = "KIAN-PC";
$connectionInfo = array( "Database"=>"T32Database");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
But a simple query like this:
$version = mssql_query('SELECT * FROM [T32Database].[dbo].[TUsers]');
$row = mssql_fetch_array($version);
echo $row[0];
Produces this output:
Fatal error: Call to undefined function mssql_query() in C:\Users\kians_000\dev\traka\index.php on line 42
phpinfo show me this:
sqlsrv
sqlsrv support enabled
Directive Local Value Master Value
sqlsrv.ClientBufferMaxKBSize 10240 10240 sqlsrv.LogSeverity 0 0
sqlsrv.LogSubsystems 0 0 sqlsrv.WarningsReturnAsErrors On On
Edit 3:
I tried out all drivers. This is the only one that works.
Maybe something with my source is wrong.
<?php
//-----------------------------------------------
// Connect to SQL Server DB
//-----------------------------------------------
$serverName = "KIAN-PC";
$connectionInfo = array( "Database"=>"T32Database");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
//-----------------------------------------------
// Perform operations with connection.
//-----------------------------------------------
$version = mssql_query('SELECT * FROM [T32Database].[dbo].[TUsers]');
$row = mssql_fetch_array($version);
echo $row[0];
/* Close the connection. */
sqlsrv_close( $conn);
?>
My output is:
Connection established. ( ! ) Fatal error: Call to undefined function
mssql_query() in C:\Users\kians_000\dev\traka\index.php on line 42
Instead of binding try calling your procedure as a query:
mssql_query('exec SP_TPL_DeleteUser #ExternalFieldID = ' . $ExternalFieldID, $con);
I mixed together two different drivers.
There are mssql and sqlsrv drivers.
Check if the statements begin with mssql_ or sqlsrv_
I use MSSQL Express 2012 and it seems like the mssql drivers aren't used anyway.
This here works now (as a hello world):
<?php
//-----------------------------------------------
// Connect to MSSQL-DB
//-----------------------------------------------
$serverName = "KIAN-PC";
$connectionInfo = array( "Database"=>"T32Database");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
//-----------------------------------------------
// Perform operations with connection.
//-----------------------------------------------
if( $client_info = sqlsrv_client_info( $conn)) {
foreach( $client_info as $key => $value) {
echo $key.": ".$value."<br />";
}
} else {
echo "Error in retrieving client info.<br />";
}
/* Close the connection. */
sqlsrv_close( $conn);
?>
Thanks for everybodys help anyway!
If you're using WAMP, make sure that the mssql module is enabled. It will have a checkmark next to its name in the context menu if it is.