将HTML网页连接到SQL Server

问题描述:

我试图在网页上显示来自我的Azure SQL数据库的表。 我一直在环顾四周,似乎无法弄清楚为什么这不起作用。 这是我连接到数据库的地方:

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" type="text/css" href="Style.css">
</head>
<?php
$host="myname.database.windows.net";
$username="theUser";
$password="password";
$database="databaseName";
$tbl_name="tableName";

$mysql = mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db($database);
$sql='SELECT * FROM $tbl_name';
$result=mysql_query($sql); 

?>

这是我创建格式化表格的地方:

<table id="MySqlTable" align="center" style="width:70%">
    <thead>
      <tr>
        <th>LiftId</th>
        <th>ItemNumber</th>
        <th>ItemRegion</th>
      </tr>
    </thead>
    <tbody>

这就是我试图用从数据库中提取信息并填充行的地方:

<? php
while($row = mysql_fetch_array($result)) { 
?>
  <tr>
    <td>
      <? php echo $row['LiftId']?>
    </td>
    <td>
      <? php echo $row['ItemNumber']?>
    </td>
    <td>
      <? php echo $row['ItemRegion']?>
    </td>
  </tr>
  <? php  
}
mysql_free_result($result);
mysql_close($mysql);
?>
    </tbody>
    </table>
    </body>

    </html>

这是我从这段代码中得到的最终产品。为什么信息没有显示在行中呢? https://i.stack.imgur.com/U5d3s.png

</div>

You're going to want to turn on some debugging and consider using mysqli as mysql is deprecated. https://docs.microsoft.com/en-us/azure/mysql/connect-php here's a good tutorial

For debugging add

ini_set('display_errors',1);
error_reporting(E_ALL);

//and
if (!$mysql) {
    die('Could not connect: ' . mysql_error());
}

to

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" type="text/css" href="Style.css">
</head>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$host="myname.database.windows.net";
$username="theUser";
$password="password";
$database="databaseName";
$tbl_name="tableName";

$mysql = mysql_connect($host, $username, $password)or die("cannot connect"); 
if (!$mysql) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database);
$sql='SELECT * FROM $tbl_name';
$result=mysql_query($sql); 

?>

In reading your question, it says connecting an HTML page to a SQL database. When I read that, it leads me to believe your HTML page has the .html extension. The PHP code required to make a MySQL connection needs a .php extension to run.