我正在使用PhP和Mysql。我对此很陌生,我想将3个表查询到html表中。
<?php $conn = mysqli_connect('localhost', 'root','') or die("Error ".mysqli_error($conn));?>
mysqli_select_db($conn, 'borok') or die("Database error: ".mysqli_error($conn));
try {
$sql = "SELECT w.wine_name
, w.wine_id
, e.user_i
, e.point
FROM wine w
JOIN evaluate e
ON e.wine_id = w.wine_id
ORDER
BY e.wine_id
, user_id; SELECT username FROM user; SELECT username from user";
} catch (Exception $e) {
$error = $e->getMessage();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Értékelések</title>
</head>
<body>
<h1>Wines</h1>
<?php if (isset($error)) {
echo "<p>$error</p>";
} else {
$conn->multi_query($sql);
do {
$result = $conn->store_result();
$row = $result->fetch_assoc();
echo"<table>
<tr>
<th></th>
<th>{$row['wine_name']}</th>
</tr>
<tr>
<td>{$row['username']}</td>
<tr>{$row['pontszam']}</tr>
</tr>
</table>";
$result->free();
} while ($conn->next_result());
}
$conn->close();
?>
</body>
</html>
我有3个表,我想使用用户名,wine_names和点。每个用户都可以评估任何葡萄酒。这些通过ID连接。例如,user1正在评估wine1,而我想在“点”行上显示该值。
在这里,我要制作这样的表并显示结果:
-----------------------------------------------------------------------------
| | wine_name1 | wine_name2 | wine_name3
-----------------------------------------------------------------------------
| user1 | point | point | point
|
| user2 | point | point | point
|
| user3 | point | point | point
我应该使用foreach吗?感谢您的帮助!