I can not connect to PHP in SQL Server 2014

0

Good afternoon guys!

I created a small application to test my PHP connection with SQL Server 2014, but I can not connect. I have seen many forums on the web and even here some doubts similar to mine, but I still have not found the solution.

My code that I'm trying to connect to is the:

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

I do not know if the information is valid, but I'm using Wamp and I do not seem to have a drive to run sqlsrvr, as shown in phpinfo's print:

Can anyone help me?

    
asked by anonymous 11.09.2017 / 19:47

2 answers

0

Maybe you should try the 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

link

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

C:\xampp\php\ext

And you should add this line to your php.ini

extension = php_pdo_sqlsrv_56_ts.dll

Note : Do not 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 crash in PHP 7, and that of PHP 7 will also crash in PHP 7.1. So first make sure that you are using only the correct version in the folder ext and php.ini

Also note that PHP is compiled from two forms in Windows : thread safe

strong> and non thread safe . This also influences which extension you should use (again, do not mix garlic with bugs).

Oh, almost forgetting, as of PHP 7 we also have one compile for x64 and one for x86. Again, you have to choose the correct version here too.

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

Now support for PHP 7.1 is still in preview, and the latest is this .

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

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

Finally, if something goes wrong, you may also need to update Microsoft ODBC Driver 13 for SQL Server . SQL Server Express 2014 uses version 11 must driver for connection, and as a reminder do not know if the new version of the driver for PHP needs you to update ODBC as well.

    
11.09.2017 / 20:17
-2

Look, I do not know if it will help a lot, but I believe there are different ways to make a connection, I also use Wamp. So I guess it just can change the way it connects. 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();
            }
}
?>

Make sure to link this file with what you've done, in this case the main page. You can use an include if you prefer ...

Good Luck !! > or

    
11.09.2017 / 20:09