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);
9
  • Possible duplicate of Connection to Azure MySQL server fails due to incorrect connection string Commented Aug 20, 2018 at 10:20
  • Basically; $username = "((Database UserName))"; needs to be $username = "((Database UserName))@((Database Server Name))"; Commented Aug 20, 2018 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 Commented Aug 20, 2018 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 Commented Aug 20, 2018 at 10:58
  • 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? Commented Aug 21, 2018 at 9:14

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.