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";
}
?>