this is my first question so I’m trying my best to describe to you my problem, thanks. I develop a PHP script using sqlsrv library to connect to a SQL Server using a private IP in a Ubuntu (18.04) server using SSH, I installed the .so necessary for the library to work and modify my php.ini to recognize it. I run my PHP code in the console and it works perfectly, it throws errors when they are and fetches the data correctly. Now I’m trying to integrate this script to make a web page that could connect to the SQL Server and get data for a Login process, I have developed this part of a web page many times but this one is the first to be on a SQL Server. The web page works until it makes a call to the PHP file that manages the communication to SQL Server but it stops in specific line when it calls the function to connect to the server (sqlsrv_connect()), I debugged the script finding all the problems by the console and resolved them, but it always stops in the same line of the code when I execute the PHP in the browser (Firefox).
I’ll show you my code:
<?php class Db { private $servername; private $username; private $password; private $dbname; private $conn; function __construct($server, $user, $pass, $db) { $this->servername = $server; $this->username = $user; $this->password = $pass; $this->dbname = $db; $this->conn = false; } public function connect() { $connectionInfo = array("Database"=>$this->dbname, "UID"=>$this->username, "PWD"=>$this->password); echo $this->conn; $this->conn = sqlsrv_connect($this->servername, $connectionInfo); echo "sqlsrv_connect realizadon"; if($this->conn === FALSE) { echo "Conexión no se pudo establecern"; die( print_r( sqlsrv_errors(), true)); } } public function loginPass($email) { echo "Funcion accedidadn"; $this->connect(); echo "Conexión realizadan"; $sql = "SELECT pass FROM Users WHERE email = '{$email}'"; $query = sqlsrv_query($this->conn, $sql); echo "Query realizadon"; if($query === FALSE) die( print_r( sqlsrv_errors(), true)); if( sqlsrv_fetch( $query ) === false) die( print_r( sqlsrv_errors(), true)); echo "Logica realizadan"; return sqlsrv_get_field($query, 0); } } ?>
And this one a more simplify implementation of the SQL connection:
<?php $serverName = "localhost"; //serverNameinstanceName $connectionInfo = array( "Database"=>"name", "UID"=>"user", "PWD"=>"pass"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { $sql = 'SELECT data FROM Table WHERE ID = 2'; $query = sqlsrv_query($conn, $sql); sqlsrv_fetch($query); $data = sqlsrv_get_field($query, 0); echo $data."n"; }else{ echo "Conexión no se pudo establecern"; die( print_r( sqlsrv_errors(), true)); } ?>
Either of the codes works executed by the console, but no for a bowser execution. In the first code, I call the LoginPass function, but it stops working when it calls connect(), more specifically in this line:
$this->conn = sqlsrv_connect($this->servername, $connectionInfo);
It doesn’t throw me an error, it just stops the execution and does anything after that line. I tried to change the permissions of the file with chmod and changing the user to www-data with chown but I get the same result. Also i run it in my personal computer (Ubuntu 18.04) and don’t work either (Firefox browser).
¿Does this problem is because a SQL configuration?
Advertisement
Answer
I fixed by reinstalling sqlsrv driver, following the instructions of Microsoft from here: https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15 Follow all the steps even if you already have installed the component, that’s how I could fix the connection to the SQL server using whatever browser.