如何将HTML连接到Azure数据库?

时间:2019-05-21 09:41:00

标签: php mysql azure-mysql-database

我正在尝试连接Azure Sql存储库和html,以显示我拥有的所有内容。但我遇到了麻烦。我研究了w3school和其他资源,但我仍然不知道怎么了?

所以我使用Notepad ++并将其另存为html并使用php建立连接

这是到目前为止我的记事本+++中的代码:

 <?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";
?> 

这就是我得到的 connect_error) { die("Connection failed: " . $conn->connect_error);

我不知道我在哪里弄错了。请帮助我,谢谢

2 个答案:

答案 0 :(得分:1)

由于我不是PHP开发人员,所以我不确定100%,但是Microsoft在其Azure文档(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);
?>

我的猜测是您的PHP格式错误,并且您正在获取代码的呈现,而不是其实际上没有正确执行-请务必阅读文档。

答案 1 :(得分:0)

我也尝试过此代码

<?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);
?>

它返回一个空白页,我觉得有些方法只是跳过了我所有的PHP代码