我正在尝试将SQL查询的结果作为表格输出到我网站的页面上。我在网上找到了一些解决方案,但我无法让它们正常工作。现在我复制并粘贴了一些代码来输出前两列,但我无法弄清楚如何获取表中的每一列。我是PHP和Web开发的新手,所以任何帮助将不胜感激。
我的PHP:
<?php
SESSION_START() ;
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$_session['userid'] = $userlogged;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid'] . "'";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climb-id"]. "</td><td>" . $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
答案 0 :(得分:0)
用var_dump检查:
有些人喜欢这样:
$result = mysqli_query($conn,$sql);
var_dump($result);
if ($result->num_rows > 0) {
也许是查询错了。