Error while trying to connect bd MSSQL Server using PDO

Asked

Viewed 288 times

1

I cannot access the MS SQL SERVER database using PDO. I’ve done everything, the best answer I could was with the code below, but I keep getting an error reply:

SQLSTATE[] Connection Error (null) (Severity 0)

My server is Centos 5.11 (64 bits)

Any idea?

<?php
  try {
    $hostname = "db-gadel-campinas.lexos.com.br,1500";
    $dbname = "LexManager_GadelCampinas";
    $username = "gadelview";
    $pw = "password";
    $pdo = new PDO ('dblib:host=$hostname;dbname=$dbname', '$username', '$pw');
  } catch (PDOException $e) {
    echo "Erro de Conexão " . $e->getMessage() . "\n";
    exit;
  }
?>

I changed the code and it seems to work the connection with sqlserver . Now is to find out how to query the table and show the data :). Any hint?

The code is correct?

<?php
  try {
    $hostname = "db-gadel-campinas.lexos.com.br";
    $port = 1500;
    $dbname = "LexManager_GadelCampinas";
    $username = "gadelview";
    $pw = "password";
    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
    echo "Conexão ok";
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}
?>
  • Which error is giving? Do not use the mysql_* he is depreciated, I think his best option is to hire a freelance.

  • PS: 1500 should probably be the Mysql port that was changed and not part of the pro server address.

  • @Thanks for the help in my question. We want to hire a freelance to create an app, but we need to have a template to present to the sponsor, so I’m racking my brain to create this MVB. If you can give me a few tips, I’ll research and test around here.

  • 1

    About a year ago I did a project to help beginners with database operations, using PDO (latest driver for database). You can access this link: https://github.com/leonardovilarinho/MinPDO in it there is a brief tutorial and how to use, I hope it helps. To make the connection is very simple, as well as consult and change records, in the index.php file of the repository has a description of how everything works.

  • I’ll take a look, thank you! But back to 1500, how should I use this information, I mean, how I declare this door in the code?

  • If it really is the door just change to db-gadel-campinas.lexos.com.br:1500 If you continue with error try only with db-gadel-campinas.lexos.com.br. And in case there’s still an error, post what error is giving.

  • db-gadel-campinas.lexos.com.br:1500 Error: Connection failed: SQLSTATE[HY000] [2005] Unknown Mysql server host 'db-gadel-campinas.lexos.com.br:1500' (1) db-gadel-campinas.lexos.com.br Error: I’ve been waiting for a few seconds without server response.

  • Without the port, the error is another: Error 503 Backend fetch failed Backend fetch failed Guru Meditation: XID: 33940816 Varnish cache server

  • Strange, that error without the port, it seems that found the database host, but gave some internal error there.

  • I already asked a question to the staff that generated the access, let’s see. Thank you.

  • I received a feedback from them, the problem is that they use SQL Server :| "The interface you are using points to a Mysql connection string... Our database is SQL Server, you will probably be able to use with the mssql_connect driver, or sqlsrv_connect..."

  • 1

    You can use $dbh = new PDO("sqlsrv:Server={$host};Database={$db}", $user , $pass); Now yes, in the $host must have the ',1500'

  • Or like: http://answall.com/questions/73279/howto connect-php-com-o-sqlserver-do-pdo

  • @lvcs At least it gave connection error :) the message that appears now is "Connection Error could not find driver" Dei a searched and it seems that my server (Locaweb) is not compatible. Any idea?

  • I switched to dblib '$Pdo = new PDO ("dblib:host=$hostname;dbname=$dbname", '$username', '$Pw');' now the error is SQLSTATE[] Connection Error (null) (Severity 0)

  • The first error is what database driver is not installed on the machine, the second you can try to fix by putting db-gadel-campinas.lexos.com.br:1500 (two points).. If this attempt fails, then I do not know how to proceed anymore.

  • @lvcs I’m using your project on Github, I made the settings according to the guidelines, I did a BD connection test, it all worked out fine, but now, when I try to run the query(), for example, I get an error Error:SQLSTATE[HY000] [2002] Can’t connect to local Mysql server through socket '/var/lib/mysql/mysql.Sock' (2) Do you have any idea what might be going on? [http://pethood.com.br/pontestore/MinPDO-master/ex/consultar.php]

Show 12 more comments

1 answer

1

When it runs the script in windows I believe it uses the comma so (dblib:host=HOST,PORTA):

new PDO('dblib:host=db-gadel-campinas.lexos.com.br,1500;dbname=<banco>', '<usuario>', '<senha>');

If it’s linux (or like-Unix) I think it’s right to use :, thus:

new PDO('dblib:host=db-gadel-campinas.lexos.com.br:1500;dbname=<banco>', '<usuario>', '<senha>');

Out that you used ' apostrophes and tried to pass variables like this '$variavel', php does not recognize this as a variable, it will think it is just a string, i can then concatenate or use normal quotes "

Do it:

$hostname = "db-gadel-campinas.lexos.com.br:1500";
$dbname = "LexManager_GadelCampinas";
$username = "gadelview";
$pw = "password";
$pdo = new PDO ('dblib:host=' . $hostname . ';dbname='. $dbname, $username, $pw);

Or with quotation marks (the other parameters do not need quotation marks)

$hostname = "db-gadel-campinas.lexos.com.br:1500";
$dbname = "LexManager_GadelCampinas";
$username = "gadelview";
$pw = "password";
$pdo = new PDO ("dblib:host=$hostname;dbname=$dbname", $username, $pw);

If the problem is really with the comma, you can do this:

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $hostname = "db-gadel-campinas.lexos.com.br,1500";
} else {
    $hostname = "db-gadel-campinas.lexos.com.br:1500";
}
  • I changed the code, I did all the tests, but it hasn’t worked yet. Either no message appears, or there is a connection error. Thanks for the help. Maybe it just works by creating an api. I’m trying. Thank you.

  • @Állanfidelistoledo but noticed that you had used the wrong host name, it is likely that it is a typo your.

  • I talked to my SAP provider, who created the database view, and he told me that I will hardly be able to make this connection by PHP, he suggested creating a C# api that accesses the bd and exports to mysql, so I will have more facility to develop the application. Do you have any idea / indication of how to do this api? I’ve searched a lot and could not solve.

  • I would even recommend using the PHP SQLSRV as PHP driver for MS SQL Server. But to answer your question: with Visual Studio 2015 you can (actually, previous versions too). Use ASP.NET Visual C#, if you want to follow the indication of your supplier. On the Microsoft website you can find step-by-step tutorials how to build simpler Apis for demonstration purposes (no production here).

  • @Guillhermenascimento Hundreds is a Red Hat Linux distribution

  • 1

    @Yes, I know, I know.

Show 1 more comment

Browser other questions tagged

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