PHP warning error

Category: azure database for mysql

Question

Pranjal Bhatia on Fri, 04 Jan 2019 05:48:54


I am working on a project that requires me to send data to a website which further sends the data to be logged into a database. I keep getting this error in the log directory of website : PHP Warning: mysqli_connect(): (HY000/9002): The server name you tried cannot be found. Please use the correct name and retry. Please check your server name waterquality

Here is the code -

<?php //An external file for storing the credentials for accessing database

$username = "pranjal@waterquality";

$password = "--------";

$database = "waterquality";

$server = "http://waterquality.database.windows.net ";

//Insert your credentials here /*WARNING: MAKE SURE NO EMPTY LINE IS AROUND AFTER ?> SIGN*/ ?>

Replies

CHEEKATLAPRADEEP-MSFT on Fri, 04 Jan 2019 06:21:52


You may use the following code to connect Azure Database for MYSQL using PHP.

<?php

$host = 'mydemoserver.mysql.database.azure.com';

$username = 'myadmin@mydemoserver';

$password = 'your_password';

$db_name = 'your_database';

//Establishes the connection

$conn = mysqli_init();

mysqli_real_connect($conn,
$host, $username, $password, $db_name, 3306);

if
(mysqli_connect_errno($conn)) {

die('Failed to connect to
MySQL: '.mysqli_connect_error());

}

For more details, refer ā€œAzure Database for MYSQL: Use PHP to connect and query dataā€.

And also, you may refer SO thread, which addressing similar issue.

Hope this helps.

Pranjal Bhatia on Fri, 04 Jan 2019 06:53:14


Getting this error:

HP 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 waterquality. in D:\home\site\wwwroot\username_password.php on line 16

Line 16: $host, $username, $password, $db_name, 3306);

I have input the name correctly, I am using the same details to connect via Microsoft SQL server management studio.

CHEEKATLAPRADEEP-MSFT on Mon, 07 Jan 2019 06:15:20


Iā€™m able to use PHP to connect Azure Database for MySQL as mentioned in the above post.

Make sure the to use the syntax as mentioned in the article.