What is the in built function or super global to get sql database server hostname using php?

StackOverflow https://stackoverflow.com/questions/19464345

  •  01-07-2022
  •  | 
  •  

Вопрос

I wanted to connect php file to be connected with a sql server database. I got the error message saying connection string failed and check database instance name or hostname.

$serverName = "00.000.000.000:1234"; //serverName\instanceName
$connectionInfo = array( 
         "Database"=>"rmrwordpress","UID"=>"rmrdbadmin", "PWD"=>"*****"); 
$conn = sqlsrv_connect($serverName, $connectionInfo);

Hosting providing team is not helping and I had spent 11days asking for this. I decided to use a php function which can get hostname of database server and instance of SQL server 2012 or 2008. So that I can use that for my database connection.

  1. I had used servername, computer name and IP in the place of $servername one after the other but didn't worked.

So please share your knowledge about an in-built function or super global which can return the hostname which can be similar to

echo gethostname();

OR

php_uname('n');

What is the in built function or super global to get sql database server hostname using php?

Это было полезно?

Решение

A lot of hosting companies don't have the sql server on the same host as the web-servers. getting the name of the host that the php script is running on may not help you.

You can try looking at the $_SERVER['SERVER_ADDR'] information in the $_SERVER array. But that would be the same as just using 'localhost' for your server address.

But php has no way of guessing where an SQL server might reside, because it could be anywhere on the internet/LAN/localhost

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top