'Connecting an HTML webpage to a SQL Server
I am attempting to display a table from my Azure SQL database on a webpage. I have been looking around and can't seem to figure out why this isn't working
This is where I am connecting to the database:
<!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);
?>
This is where I create a formatted table:
<table id="MySqlTable" align="center" style="width:70%">
<thead>
<tr>
<th>LiftId</th>
<th>ItemNumber</th>
<th>ItemRegion</th>
</tr>
</thead>
<tbody>
This is where I am trying to fill the rows with the information being pulled from the database:
<? 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>
This is the final product that I am getting from this code. Why is the information not being displayed in the rows?? https://i.stack.imgur.com/U5d3s.png
Solution 1:[1]
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);
?>
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | PHPDave |
Solution 2 | Gagich |