I can’t connect PHP to SQL Server 2014

Asked

Viewed 604 times

0

Good afternoon, you guys!

I created a small application to test my PHP connection with SQL Server 2014, but I’m not able to connect. I’ve seen numerous forums on the web and even here some questions similar to mine, but I still haven’t found the solution.

My code I’m trying to connect to the bank is:

<?php
   $connect = odbc_connect("araguaina","sa","@@2013DOT**flex");
   $sql = "SELECT * FROM cd_empresa";
   $result = odbc_exec($connect, $sql);
?>

I don’t know if the information is valid, but I’m using Wamp and I don’t seem to have a drive to run sqlsrvr, as the phpinfo print shows:

inserir a descrição da imagem aqui

Someone can help me?

2 answers

0


Maybe you should try PDO with the Sqlsrv plugin (this is what I’m using to connect to my other boss’s software that uses the SQL Server 2014 database):

$connection = new PDO("sqlsrv:Server=" . $server . ";Database=" . $database, $username, $password);
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

You can download the plugin here

https://www.microsoft.com/en-ca/download/details.aspx?id=36434

In Xampp, you should copy the dll in the folder:

C:\xampp\php\ext

And you should add this line to your php.ini

extension = php_pdo_sqlsrv_56_ts.dll

Note: Don’t forget to restart your server so that it can take into account the new php.ini file.


OBS. PHP 7

PHP extension Dlls are compiled to work in a single version of PHP. The PHP 5.6 DLL will lock in PHP 7, and the PHP 7 DLL will lock in PHP 7.1. So, at first, make sure you’re using only the correct version in the ext and in the php.ini

Also note that PHP is compiled from two ways in Windows: thread safe and non thread safe. This also influences which extension you should use (again, don’t mix garlic with oranges).

Ah, almost forgetting, from PHP 7 we also have a compilation for x64 and another for x86. Again, you need to choose the correct version here too.

Assuming you’re using the version Thread Safe (the one that comes along with XAMPP/Apache generally), download the latest extension to work with SQL Server. The easiest way is look at the driver github repository, in the releases part and download only the version you want.

Today support for PHP 7.1 is still in preview, and the latest version that’s the one.

Download only the zip for PHP 7.1, choose the architecture, thread safe or not, and extract only them in the folder ext and insert into php.ini.

// suporte PDO
extension=php_pdo_sqlsrv_71_ts.dll
// sem PDO
extension=php_sqlsrv_71_ts.dll

Finally, if something doesn’t work out, you may need to update the Microsoft ODBC Driver 13 for SQL Server. SQL Server Express 2014 uses version 11 must driver for connection, and so remind me not know if the new version of the driver for PHP needs you to update the ODBC as well.

  • I’ve done all the procedures you described above, just didn’t find the php.ini file in the PHP installation folder inside Xampp. The files I found were: php.ini-Development, php.ini-Production and added the PDO support extension to them (my php is Thread Safe enabled). Even so I still can’t access the sql server :(

  • php.ini in php folder: https://imgur.com/a/uvfJd and by the panel of the xampp: https://imgur.com/a/XwSgY

  • I’ve already added this file as well. See if I got it right with the dot and comma in front: ;Extension=php_pdo_sqlsrv_71_ts.dll

  • .removes the ;

  • Now another error has appeared: Fatal error: Uncaught Pdoexception: could not find driver

  • I added the following dlls: [https://imgur.com/a/h9MWT

  • The version of my php is 7.1.8

  • This error occurred because it did not find the SQL Server drive. EM Extension=php_pdo_sqlsrv_71_ts.dll puts Extension=php_pdo_sqlsrv_71_ts_x64.dll

  • I put it in the php.ini file as well?

  • is not missing an "L" at the end? Extension=php_pdo_sqlsrv_71_ts_x64.dl

  • @Diegogrossi Yes. Fixed. this code is in php.ini

  • He’s returning the same mistake. I no longer know what I do :( Fatal error: Uncaught Pdoexception: could not find driver in C: xampp htdocs highway-Araguaina-testing-sql test.php:7

  • Please see if my code is correct? <?php&#xA;$server = "192.168.1.11";&#xA;$database = "ARAGUAINA";&#xA;$username = "sa";&#xA;$password = "@@2013DOT**flex";&#xA;&#xA;$connection = new PDO("sqlsrv:Server=" . $server . ";Database=" . $database, $username, $password);&#xA;$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);&#xA;?>&#xA;

  • Yes it is. in php.ini change php_pdo_sqlsrv_71_ts_x64.dll by the name of the other drives you installed to view

  • Friend, I believe I was able to connect to the bank, because I did not return any error. I just need to know how to query the bank using the PDO connection. Would be: function exibeUltimoElemento($conexao){&#xA;&#xA; $query = "select Cd_Empresa from cd_empresa";&#xA; $resultado = pdo_exec($conexao, $query);&#xA; $empresas = 0;&#xA;&#xA; while ($empresa_array = pdo_fetch_row($resultado)) {&#xA; $empresas ++;&#xA; }&#xA;&#xA; return $empresas;&#xA;}

  • An example in image: http://imgur.com/a/WZ0kK . A tutorial on PDO: http://www.devmedia.com.br/introducao-ao-php-pdo/24973 Note: PDO commands are the same for all databases. what can change is your SQL query

  • 1

    My friend, thank you so much! You saved my day! Problem solved, I can now access and consult my bank! Thanks again! ;)

  • I’m new to the Forum. Where I put the topic as solved?

  • http://imgur.com/a/DWe55

  • 1

    Solved! Thanks a lot!

Show 15 more comments

-2

Look, I don’t know if it’s gonna help much, but I believe there are different ways to make a connection, I also use Wamp. So I think you can just change the way you connect. For example:

<?php

function getConnection(){
    $servidor = "localhost";
    $usuario = "usuario";
    $senha = "123456";
    $banco = "programacao";
    $link = mysql_connect($servidor, $usuario, $senha);
    $db = mysql_select_db($banco,$link);
        if(!$link)
            {
                echo "erro ao conectar ao banco de dados!";exit();
            }
}
?>

Be sure to link this file with what you did, in case the main page. You can use a include if you prefer...

Good Luck !! >o<

  • He needs to connect to SQL Server 2014 not in the mysql

  • Alis, my code with your suggestion looks like this: //conexao com o sql &#xA;$host = "192.168.1.11"; $user = "as"; $pass = "@@2013DOT**flex"; $banco = "ARAGUAINA"; $conexao = mssql_connect($host, $user, $pass) or die(mssql_get_last_message()); mssql_select_db($banco,$conexao) or die (mssql_get_last_message());

  • But it brings me the same error when I try to connect via odbc_connect. Error follows: Fatal error: Uncaught Error: Call to Undefined Function mssql_connect()

Browser other questions tagged

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