I wonder if anyone can help me. I'm creating an application in PHP and am using a SQL server database on my local computer to develop the application. The only problem is I cannot connect to the database. My code looks like:
<?php
$serverName = "LIAMJAY-PC\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"ONEDB");
$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));
}
?>
to connect to the database. However, everytime I run the code I keep getting a 18456 error code. Does anybody know what my problem is and if so, what is the solution???
Try supplying UID and PWD as follows:
$connectionInfo = array( "Database"=>"ONEDB", "UID"=>"userName", "PWD"=>"password");
Error code 18456 occurs when you are using SQL Server Authentication and the login name and password are incorrect, or even when the user is disabled or locked.
Related
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.
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
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 want to connect to a specific database on SQL Server Through PHP. On WAMP Server, I have PHP 5.6 installed so I downloaded SQLSRV32.EXE (Microsoft Drivers for PHP for SQL Server) then I copied these files in C:\wamp64\bin\php\php5.6.25\ext
IN php.ini, I added
extension=php_sqlsrv_56_nts.dll
extension=php_sqlsrv_56_ts.dll
But when i check phpinfo() on browser i don't see the sqlsrv module listed.
When i try to connect to my SQL Server Database I get this error
Fatal error: Call to undefined function sqlsrv_connect()
Here is my code
<?php
$serverName = "PC0CFEP2\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"=>"test");
$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 was checking my solutions on different forums and I tried many but I still can't get it work. I am sure it's in some settings but i couldn't find out what could be the issue.
Any suggestion please ? Thank you very much.
At First, Please run MSSQL server database.
In php.ini file, Please added below code
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
and keep two dll file \ext folder
To connect database , try
$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));
}
Remove "php_" prefix:
extension=sqlsrv_56_ts.dll
extension=pdo_sqlsrv_56_ts.dll
This worked for me. Thanks for asking this question.
I have the following code that doesn't seem to make the connection to the MSSQL db:
<?php
$serverName = "SEVER_NAME\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"TESTDB",
"UID"=>"SERVER_NAME\Administrator",
"PWD"=>"Password123");
$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));
}
?>
The message reads that login failed for user SERVER_NAME\Administrator, but that's what I see in the SQL Server Management Studio when I check the DB properties. Any help is greatly appreciated.
The login name looks suspiciously like that SQL server is using Windows authentication. If this script and the SQL server are on the same machine, try omitting UID and PWD entirely, as per the documentation.
If values for the UID and PWD keys are not specified in the optional $connectionInfo parameter, the connection will be attempted using Windows Authentication.