Problems connecting from apache (ubuntu) to sql server using PHP

0

I will try to explain: I want to connect from a server that I have xubuntu to sql server 2008 using a php query. I already have the necessary drivers and extensions installed on the server. The sample query that microsoft gives me if I can connect since it returns the server data, the problem is that with the query I had and it works in window I can not connect.

Code with which connects me perfectly:

<?php
$serverName = "172.26.7.192";
$connectionOptions = array(
    "Database" => "A3LABORAL",
    "Uid" => "consulta",
    "PWD" => "Monte00!"
);

//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if( $conn === false ) {
    die( FormatErrors( sqlsrv_errors()));
}

//Select Query
$tsql= "select * from [master].[dbo].[ZMontesano_Vista_Agenda]";

//Executes the query
$getResults= sqlsrv_query($conn, $tsql);

//Error handling
if ($getResults == FALSE)
    die(FormatErrors(sqlsrv_errors()));
?>



<?php
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
    echo ($row['SQL_VERSION']);
    echo ("<br/>");
}

sqlsrv_free_stmt($getResults);


function FormatErrors( $errors )
{
    /* Display errors. */
    echo "Error information: <br/>";
    foreach ( $errors as $error )
    {
        echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";
        echo "Code: ".$error['code']."<br/>";
        echo "Message: ".$error['message']."<br/>";
    }
}
?>

Problem when using PDO and Working from local in window:

<?php
    echo "nada";
    $pdo=new PDO("sqlsrv:Server=172.26.7.192;Database=A3LABORAL", "consulta", "Monte00!");
    $statement=$pdo->prepare("select * from [master].[dbo].[ZMontesano_Vista_Agenda]");
    $statement->execute();
    if (!$statement){
        echo 'Error al ejecutar la consulta';
    echo "no entro";
    }else{
        $results = $statement->fetchAll(PDO::FETCH_ASSOC);
        echo  json_encode($results);
    echo "entro";

    }
    ?>

I leave a sample image: Thanks

    
asked by Yasiel Hernández 26.04.2018 в 15:04
source

1 answer

0

To connect your linux apache server to a database server in windows there are several ways one of them is to use FreTDS:

#sudo apt-get install -y unixodbc unixodbc-dev unixodbc-bin libodbc1 
odbcinst1debian2 tdsodbc php5-odbc
#sudo apt-get install -y freetds-bin freetds-common freetds-dev libct4 
libsybdb5

Then you must configure it to connect to your Database host, I leave you the link of laninformacion for you to review. It's a bit complicated but if possible:

link

    
answered by 26.04.2018 в 15:30