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
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.
PHP version:7.4, SQL Server version:2019,
I was going to try to specify port in connection but it didn't work.
<?php
$serverName = "MYPCNAME, 1433";
$connectionInfo = array( "Database"=>"MyDB", "UID"=>"sa", "PWD"=>'MyPassword');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Above showed below message.
[Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: An existing connection was forcibly closed by the remote hos
When I removed port like this $serverName = "MYPCNAME" , it worked well.
I changed port to 1434, and tested again (Of course I restarted SQL Server, firewall set).
$serverName = "MYPCNAME, 1434"
This gave me same error but $serverName = "MYPCNAME" This worked well.
I tried to specify instance name too, but there was same issue.
I installed php_sqlsrv, php_pdo_sqlsrv well and in phpinfo(), they are displayed as installed successfully.
Is there anything what I missed?
Can anyone help me?
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"
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
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.