PHP and Sqlserver connection

Asked

Viewed 944 times

3

I’m trying to make a connection to Sqlserver and PHP But the page returns me the following error:

Warning: mssql_connect() [Function.mssql-connect]: Unable to connect to server: 192.168.2.7 SRVDOC DOCSYSTEMSCAN in C: wamp www conexao.php on line 2

Warning: mssql_select_db() expects Parameter 2 to be Resource, Boolean Given in C: wamp www connected.php on line 3

The current code is:

<?php
    $con = mssql_connect("192.168.2.7\SRVDOC\DOCSYSTEMSCAN", "sa", "minhasenha");
    mssql_select_db("fd_585b0f87", $con);
 ?>

I’m racking my brain, but I don’t know what it is... any suggestions? Thank you.

  • is SQL Express ?

  • Hello, not full SQL friend

  • put only ip does not give ?

  • This is a question I have... There I should put the server... But what is it? The ip or the server name I log in to Sqlserver?

  • <?php &#xA;$conn = mssql_connect("ADMIN-PC\SQLEXPRESS", "usuario", "senha") or die ("erro ao conectar"); where ADMIN-PC is the name of your machine and SQLEXPRESS the name of your instance of SQL Server. It’s just like when you’re logging in through the SQL Server Manager

  • Dude, you have configured Sqlserver for this type of connection before. It is necessary to release port 1433 and the form of connection per ip within Sqlserver.

Show 1 more comment

3 answers

1

Friend, try following the proposed template on the official PHP manual page:

$serverName = "SRVDOC\DOCSYSTEMSCAN"; //serverName\instanceName
$connectionInfo = array( "Database"=>"fd_585b0f87", "UID"=>"sa", "PWD"=>"minhasenha");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

Reference: http://php.net/manual/en/function.sqlsrv-connect.php

Or try this way with the function "mssql_connect"

$server = 'SRVDOC\DOCSYSTEMSCAN';
$link = mssql_connect($server, 'sa', 'minhasenha');

Reference: http://bg2.php.net/manual/en/function.mssql-connect.php

Good luck!

0

According to the job documentation mssql_select_db one likely cause of this error may be the underscore contained in the name of the database.

To escape the name of a database that contains spaces, hyphens ("-"), or any other exceptional characters, the name of the database must be in brackets, as shown in the example below. This technique should also be applied when selecting a database name that contains a reserved word (such as Primary).

To escape the character put the fd_585b0f87 in square brackets, like this:

<?php
    $con = mssql_connect("192.168.2.7\SRVDOC\DOCSYSTEMSCAN", "sa", "minhasenha") 
    or die ("Erro ao conectar-se ao BD: ".mssql_get_last_message());
    mssql_select_db('[fd_585b0f87]', $con);
?>

0

You could try using the PDO to Sqlserver

<?php
  try {
    $hostname = "myhost";
    $port = 10060;
    $dbname = "tempdb";
    $username = "dbuser";
    $pw = "password";
    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.