1

I have an issue connecting to an Azure Database in PHP and I can't seem to figure out why. I am running Apache on XAMPP and trying to execute following code.

$servername = "((Database Server Name)).database.windows.net";
$username = "((Database UserName))@((Database Server Name))";
$password = "((Database Password))";
$dbname = "((Table Name))";

// Create connection
$conn = mysqli_init();
mysqli_real_connect($conn, $servername, $username, $password, $dbname, 3306);

if (mysqli_connect_errno($conn))
{
    die('Failed to connect to MySQL: '.mysqli_connect_error());
}

echo "Connected successfully";
mysqli_close($conn);
?>

The full error message i get:

Warning: mysqli_real_connect(): (HY000/9002): The server name you tried cannot be found. Please use the correct name and retry. Please check your server name ((Server Name)). in C:\xampp\htdocs\GetFromDBTest\getfromdb.php on line 16 Failed to connect to MySQL: The server name you tried cannot be found. Please use the correct name and retry. Please check your server name ((Server Name)).

line 16 is:

mysqli_real_connect($conn, $servername, $username, $password, $dbname, 3306);
Lardman
  • 33
  • 1
  • 7
  • Possible duplicate of [Connection to Azure MySQL server fails due to incorrect connection string](https://stackoverflow.com/questions/44035710/connection-to-azure-mysql-server-fails-due-to-incorrect-connection-string) – IsThisJavascript Aug 20 '18 at 10:20
  • Basically; `$username = "((Database UserName))";` needs to be `$username = "((Database UserName))@((Database Server Name))";` – IsThisJavascript Aug 20 '18 at 10:22
  • You still need servername. let me clarify a little more. If your full server path is `myLittleDb.database.windows.net` then your username is now `((Database UserName))@myLittleDb` – IsThisJavascript Aug 20 '18 at 10:48
  • have tried adding the @DB Server name to the user string which gave me something new to chew on because it now says that the server cannot be found – Lardman Aug 20 '18 at 10:58
  • Are you able to make a connection via cli or a MySQL tool? – IsThisJavascript Aug 20 '18 at 11:00
  • Tried to connect through MySQL tools and i get the same error – Lardman Aug 20 '18 at 11:09
  • Are you trying to connect to Azure SQL Database or Azure Database for MySQL? Your code is trying to connect to an Azure SQL DB server, which uses the "database.windows.net" syntax. The Azure DB for MySQL service uses the "server.mysql.database.azure.com" syntax. – andrea-lam-MSFT Aug 20 '18 at 21:14
  • I was not aware there were different types of Azure Databases. It's an Azure SQL Database. will try to figure out how to connect to that type insted :) thx for the info – Lardman Aug 21 '18 at 07:09
  • 1
    Have fixed my issue, I was trying to connect to an Azure MySQL DB but i have an Azure MSSQL DB, different setup using sqlsrv_connect insted of mysqli_real_connect and ofc with respected parameters. got it working. Is there any way i can mark this as solved or should i just delete this post? – Lardman Aug 21 '18 at 09:14

1 Answers1

0

This issue was fixed, it was a case of lack of knowledge from my side. I had no idea Azure offered different types of Databases.

The solution: I was using an msSQL DB and i tried to connect to it with mySQL. Solved by using msSQL to connect to my DB

Here is the msSQL version for anyone who may need it.

$serverName = "<DB Server Name Goes here>";
$connectionOptions = array("Database" => "<DB Name Goes here>", 
    "Uid" => "<User Id for the DB goes here>", 
    "PWD" => "<Password connected to the user Id goes here>");

$conn = sqlsrv_connect($serverName, $connectionOptions);
$tsql= "SELECT * FROM dbTable";
$getResults= sqlsrv_query($conn, $tsql);
if ($getResults == FALSE)
    echo (sqlsrv_errors());

while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC))
{
    //Here you deal with your data
}
//closes sql connection and clears everything connected to it
sqlsrv_free_stmt($getResults);
Lardman
  • 33
  • 1
  • 7