I've been struggling with this for a ridiculous amount of time and I'm not sure what to do. I'm using PHP 5.4 on a GoDaddy Linux shared hosting account with the mssql module enabled. The database I'm trying to reach is hosted elsewhere. I'm trying to connect via mssql_connect. I've tried
$con = mssql_connect('<ip address>', 'user', 'pass');
as well as
$con = mssql_connect('<ip address>\<instance name>', 'user', 'pass');
And the above two with port numbers. But I still get the "Unable to connect to server" error. I've made sure SQL Server is accepting outside connections and SQL Agent is running. I can't use PDO with sqlserv because of GoDaddy, mssql is the only module I'm able to use.
Does anyone have any ideas as to what could be wrong?
Try using SQLSRV functions:
<?php
$serverName = "ipAddress\instanceName";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"user", "PWD"=>"pass" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "INSERT INTO Table_1 (id, data) VALUES (?, ?)";
$params = array(1, "some data");
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
?>
Note: you don't need to inform the instance name in some cases. Try with IP address alone first.
Related
I'm using WAMP to run my code locally.
Now I want to connect to a microsoft SQL server database using PHP.
// connection with callstats DB
$serverName = "IP, 1433"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"CDR", "UID"=>"username", "PWD"=>"password");
Im using:
$conn_CDR = sqlsrv_connect( $serverName, $connectionInfo);
$params = array();
And then for a query:
$result = sqlsrv_query($conn_CDR, "", $params);
But then I get this error:
Warning: sqlsrv_query() expects parameter 1 to be resource, boolean given
Now I know I need to install sqlsrv into my localhost.
I'm using PHP 7.2.14 so I installed php_sqlsrv_72_ts.dll to my C:\localhost\bin\php\php7.2.14.
Also I added this line to my php.ini.
extension=php_sqlsrv_72_ts.dll
Then I restarted my WAMPserver, and runned my code again, but I still get the same result.
I also tried adding the file to C:\localhost\bin\apache\apache2.4.37\binbut no succes either.
Does anyone know why this error keeps occurring and how to fix it?
I solved it by installing the Microsoft ODBC Driver for SQL Server onto my computer.
Then I restarted my WAMP server and it worked!
Download ODBC Driver here
This gave me the info I needed to solve my error:
if ($conn_CDR === false) {echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);}
$conn_CDR = sqlsrv_connect( $serverName, $connectionInfo);
$params = array();
$sql = "your sql query";
$result = sqlsrv_query($conn_CDR, $sql, $params));
See the manual here https://www.php.net/manual/en/function.sqlsrv-query.php
like this example:-
$sql = "INSERT INTO Table_1 (id, data) VALUES (?, ?)";
$params = array(1, "some data");
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
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
I know it's a specific question but I'm really not sure wheter it's possible or not. With Wamp I can use the following code:
<?php
$serverName = "XXXX";
$connectionInfo = array( "Database"=>"XXXX");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if($conn === false)
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
Of course this code won't work on my ubuntu computer where Apache and PHP are running. Is there a way to specify the username and the password for the Windows authentication or do you have to use the SQL authentication ?
I have been playing around for PHP with a while. This is how i connect to sql server and it works:
$serverName = "SNAME";
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"USERNAME", "PWD"=>"PASSWORD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql = "SELECT X FROM myTable";
$query = sqlsrv_query($conn,$sql);
if ($query === false){
echo "Could not link to SQL Server";
}
while ($row = sqlsrv_fetch_array($query))
{
$ARRAY[] = "$row[X]";
}
Server SNAME is on the local network, and i can connect to it from my computer using Sql Management Studio.
Now looking online and other places I have realized PDO is the better way to go. So I have been trying to connect to my server using PDO, by following examples online. Here is the code:
$username = "USERNAME";
$password = "PASSWORD";
try{
$conn = new PDO('mysql: host=SNAME;port=1433;dbname=DBNAME',$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->query('SELECT X FROM myTable');
foreach($data as $row) {
print_r($row);
}
}
catch{
echo 'ERROR: ' . $e->getMessage();
}
When i run this i get this on my page:
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the
target machine actively refused it.
Now i know my other information is right since i can use the top block and sql management studio. Is there anyother library or something special needed. Is there problem with my code?
UPDATE
I ran this in my SQL Management Studio to check my port and it gave me 1433.
SELECT DISTINCT local_tcp_port FROM sys.dm_exec_connections WHERE local_tcp_port IS NOT NULL
In one example, you are using sqlsrv_connect, but in the other you tell PDO to connect to a 'mysql:' database.
You need to use the correct DSN string for a SQL Server connection.
$conn = new PDO('sqlsrv:Server=SNAME,1433;Database=DBNAME',$username,$password);
Manual: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php
P.S. Make sure you have PDO_SQLSRV installed. http://php.net/manual/en/ref.pdo-sqlsrv.php
I have a PHP website on a Windows machine and would like to insert data DIRECTLY to SQL database which is located on IIS server on a different machine?
Which approach shall I follow?
**My initial thought was to divert to a PHP page hosted by IIS that executes a query and then redirect back to my website. However, I prefer something easier and less complicated approach.
IIS is on Windows Server 2008 R2.
Your help is appreciated very much. Thanks!
If you're using PHP, you can open a connection to a database on another machine using PDO:
$remotedb = new PDO("*dbtype*:dbname=*dbname*;host=*addressOfOtherMachine*", $user, $password);
http://www.php.net/manual/en/class.pdo.php
I use SQLSRV in PHP to connect to a database on an IIS6 machine with 2008 R2. I believe the remote machine must allow remote connections and needs to be turned on through the settings if it isn't. Here is what my connection string looks like:
<?php
/* NEW CONNECTION FOR SQLSRV DRIVER (for MSSQL access) */
$uid = "username";
$pwd = "password";
$DB = "database_name";
$serverName = "ip_address_of_remote_server (example: 192.168.0.25)";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=> $DB, "ReturnDatesAsStrings" => true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
?>
Here is a sample parameterized insert query using the connection:
<?php
$p1 = "values passed from a form";
$p2 = "values passed from a form";
$p3 = "values passed from a form";
$params = array(
array(&$p1, null, null, SQLSRV_SQLTYPE_VARCHAR(50)),
array(&$p2, null, null, SQLSRV_SQLTYPE_DATETIME),
array(&$p3, null, null, SQLSRV_SQLTYPE_VARCHAR(50))
);
$sql = "INSERT INTO database_name (column1, column2, column3) values (?,?,?)";
$stmt = sqlsrv_prepare($conn, $sql, $params);
if( !$stmt ) { die( print_r( sqlsrv_errors(), true)); }
if(sqlsrv_execute( $stmt ) === false ) { die( print_r( sqlsrv_errors(), true)); }
?>
For more SQLSRV params, visit http://msdn.microsoft.com/en-us/library/cc626305.aspx