1
votes

I just make a connection to my database in my remote SQL Server, but unfortunately I got this error.

Constant expression contains invalid operations in public $conn = sqlsrv_connect( $serverName, $connectionInfo);

Here is my code:

<?php
    class Database{

        //specify database credentials
        private $serverName = "siber.com\\sqldeveloper, 1433"; //serverName\instanceName, portNumber (default is 1433)
        private $connectionInfo = array( "Database"=>"api_db", "UID"=>"SA", "PWD"=>"SiberCorshinee1123");
        public $conn = sqlsrv_connect( $serverName, $connectionInfo);

    public function getConnection(){

        $this->conn = null;
    }
?>
1
What is the error?Zhorov
"siber.com\\sqldeveloper" doesn't seem like the right name for a SQL Server instance.Larnu
Constant expression contains invalid operations in public $conn = sqlsrv_connect( $serverName, $connectionInfo);Abam99

1 Answers

0
votes

When you define a class member variable, it's important to know that this declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Change your PHP script like this (probably you want to use class member variables $serverName and $connectionInfo):

<?php
class Database {

    private $serverName = "sibernetik.com\\sqldeveloper, 1433";
    private $connectionInfo = array("Database"=>"api_db", "UID"=>"SA", "PWD"=>"SiberCorshine123");
    public $conn;

    public function getConnection(){
        $this->conn = sqlsrv_connect($this->serverName, $this->connectionInfo);

        // Additional code here, at least check the result from sqlsrv_connect().
    }
}
?>