1

I'm trying to connect my Azure Sql storeage and html to show everything i have. but i'm having some trouble. i researched w3school and other resources but i still don't know what is wrong?

so i use Notepad++ and save it as html and use php to establish the connection

here is the code in my notepad+++ so far:

 <?php
$servername = "servername*****.mysql.database.azure.com";
$username = "loginfor****n@mysql***";
$password = "*****";
$db = "db";
// Create connection
$conn = new mysqli($servername, $username, $password. $db);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?> 

and this is what i got connect_error) { die("Connection failed: " . $conn->connect_error);

i have no idea where did i went wrong. please help me if you can, thanks

Eddie Vu
  • 65
  • 1
  • 8
  • The proper resources to check are the docs and tutorials for [Azure DB for MySQL](https://learn.microsoft.com/en-us/azure/mysql/connect-php), not W3School. Have you tried the tutorials? What is the *actual* error? If you try to connect from your desktop without a VPN or [configuring the server firewall](https://learn.microsoft.com/en-us/azure/mysql/quickstart-create-mysql-server-database-using-azure-portal#configure-a-server-level-firewall-rule), you'll get an error – Panagiotis Kanavos May 21 '19 at 10:04
  • Can you connect to the database using [MySQL Workbench](https://learn.microsoft.com/en-us/azure/mysql/quickstart-create-mysql-server-database-using-azure-portal#connect-to-the-server-by-using-the-mysql-workbench-gui-tool) or the [mysql](https://learn.microsoft.com/en-us/azure/mysql/quickstart-create-mysql-server-database-using-azure-portal#connect-to-mysql-by-using-the-mysql-command-line-tool) tool? The docs shows how to configure the firewall and connect using either tool – Panagiotis Kanavos May 21 '19 at 10:06
  • i did actually, the my notepad++ has nothing but the php and when i run it, nothing is showing, – Eddie Vu May 21 '19 at 10:27
  • it return a blank page – Eddie Vu May 21 '19 at 10:28
  • Update the question, don't post code in the comments. Did you try using `mysqli`? If you can't connect from the command line, you won't be able to connect from PHP either – Panagiotis Kanavos May 21 '19 at 10:58

2 Answers2

1

I'm not 100% sure as I'm not a PHP dev, but Microsoft have the following on their Azure documentation (here):

<?php
    $serverName = "your_server.database.windows.net"; // update me
    $connectionOptions = array(
        "Database" => "your_database", // update me
        "Uid" => "your_username", // update me
        "PWD" => "your_password" // update me
    );
    //Establishes the connection
    $conn = sqlsrv_connect($serverName, $connectionOptions);
    $tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
         FROM [SalesLT].[ProductCategory] pc
         JOIN [SalesLT].[Product] p
         ON pc.productcategoryid = p.productcategoryid";
    $getResults= sqlsrv_query($conn, $tsql);
    echo ("Reading data from table" . PHP_EOL);
    if ($getResults == FALSE)
        echo (sqlsrv_errors());
    while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
     echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);
    }
    sqlsrv_free_stmt($getResults);
?>

My guess is that your PHP is malformed and you're getting a render of the code rather than it actually properly executing - be sure to read the documentation.

Clint
  • 6,133
  • 2
  • 27
  • 48
  • The OP is asking about [Azure DB for MySQL](https://azure.microsoft.com/en-us/services/mysql/) and no, I didn't know about it either or at least, it hasn't registered. It's [thoroughly documented](https://learn.microsoft.com/en-us/azure/mysql/) with a lot of step-by-step quickstarts and tutorials. Most likely the OP hasn't configured the server firewall – Panagiotis Kanavos May 21 '19 at 10:09
0

i've also tried this code too

<?php
$host = '****.mysql.database.azure.com';
$username = '****@mysql****';
$password = '****';
$db_name = '*****';

//Establishes the connection
$conn = mysqli_init();

printf("hello")
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
printf("sory");
die('Failed to connect to MySQL: '.mysqli_connect_error());
}

// Run the create table query
if (mysqli_query($conn, '
select * from table1;
')) {
printf("Table created\n");
}

//Close the connection
mysqli_close($conn);
?>

it returns a blank page, i feel that some how it just skip all of my php code

Eddie Vu
  • 65
  • 1
  • 8
  • i also use the account obtained from azure to do other tasks, so i know i got the user details correct – Eddie Vu May 21 '19 at 10:32
  • Update the question, don't post extra information as an answer. Can you connect using `mysqli` or `MySQL Workbench`? Did you configure the server firewall? If not, there's no point trying to connect through code – Panagiotis Kanavos May 21 '19 at 10:57