Connect SQLServer 2014 with PHP

0

I'm trying to connect SQLServer 2014 with PHP, when trying this code works

$servidor = "NOMBREPC\SQLEXPRESS";
$database = "PRUEBA";
$info = array('Database'=>$database);
$cn = sqlsrv_connect($servidor, $info);

But when trying using a class, it does not work

class Conexion{

    public function conectar(){

        $servidor = "NOMBREPC\SQLEXPRESS";
        $database = "PRUEBA";

        $info = array('Database'=>$database);
        $cn = sqlsrv_connect($servidor, $info);
        }
}

I do not know what the error is, it should be noted that in the first block of code use an if to know if it connected, but in the second when trying it does not show the message, also try with the die ().

Please help me. T.T

EDITING

In the first example if you use the sqlsrv_errors , when I changed a value that was not correct, the errors came out, but in the second I put an if, if I connected, I had to leave connected but nothing comes out, the screen goes blank. The code to call use the following:

include ("Conexion.php");

class MiClase{
   public function miFuncion(){
      $cn = new Conexion;
      $cn->conectar();
   }
}

I still can not connect using the class.

    
asked by Kevin Sandón 25.07.2016 в 05:45
source

2 answers

0

After several attempts, I managed to make the connection with SQL Server 2014, I do not know if the code I used is the correct one but managed to connect.

   <?php

        class Conexion{

            private $cn;

            public function __construct(){
                $servidor = "NOMBRE-PC\SQLEXPRESS";
                $database = "BDPRUEBA";
                $info = array('Database'=>$database);
                $this->cn =sqlsrv_connect($servidor, $info);
            }

            public function getConecta(){
                return $this->cn;
            }

        }

?>

The call to the function is as follows:

<?php include ("Conexion.php");     

    class MiClase{      

        public function miFuncion(){

        $cn= new Conexion;

            $query = "SELECT * FROM demo";
            $registros = sqlsrv_query($cn->getConecta(), $query);
                if( $registros === false ){
                    echo "Error al ejecutar consulta.</br>";
                }  else {
                        while($demo= sqlsrv_fetch_array($registros)) {
                            $listado[] = $demo;                     
                        }
                        return $listado;
                }
        }       
    }

?>

If there is an error or there is a better way to do it please communicate. Thanks

    
answered by 27.07.2016 / 03:51
source
0

It may be a problem with Server authentication, whether or not Windows validation can be used.

For the cases in which you pass the user, the password and the connection port use:

<?php
 $serverName = "serverName\sqlexpress, 1542"; //serverName\instanceName, portNumber (por defecto es 1433)
 $connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
 $conn = sqlsrv_connect( $serverName, $connectionInfo);

 if( $conn ) {
     echo "Conexión establecida.<br />";
 }else{
     echo "Conexión no se pudo establecer.<br />";
    die( print_r( sqlsrv_errors(), true));
 }
 ?>

You have more info Here

    
answered by 26.07.2016 в 09:07