dtdvbf37193 2017-12-19 10:57
浏览 48

根据变量显示行数

I have two tables, SpeciesHuntBoats contains details of Boats, the Make, Skipper etc. and SpeciesHunt which is generated by the skipper submitting a catch report form with details of their fishing catches. showboats.php displays 5 columns, Year, Boat Name, Make, Skipper, Number Of Species Caught.

For the final column I want a display of the number of rows in SpeciesHunt where that BoatName appears (to give a leaderboard of who has entered the most catches). This is as far as I've been able to get unfortunately, any help much appreciated!

<body>

<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Count Rows
$result = mysqli_query($conn, "SELECT * FROM SpeciesHunt");
$num_rows = mysqli_num_rows($result);
// Check row count of entire table works
echo "$num_rows Rows
";


$sql = "SELECT Year, BoatName, BoatMake, Skipper FROM SpeciesHuntBoats";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>Hunt Year</th><th>Boat Name</th><th>Boat Make</th><th>Skipper</th><th>Number Of Species</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["Year"]. "</td><td>" . $row["BoatName"]. "</td><td>" . $row["BoatMake"]. "</td><td>" . $row["Skipper"]. "</td><td>

$num_rows</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

$conn->close();
?>

</body>

Table structures: SpeciesHunt

SpeciesHuntBoats

  • 写回答

1条回答 默认 最新

  • duan32342 2017-12-19 12:13
    关注

    For your tables, you should rather make use of a relationship between boats and hunts

    You should research a bit into sql relationships.

    Currently you are basically saving the boat name, make and skipper twice.

    This is a better suggestion for the structure:

    Boats:

    • Year
    • Name
    • Make
    • Skipper
    • Photo

    Hunts:

    • DateCaught
    • Angler
    • Species
    • Notes
    • BoatId (This will be a reference to a certain boat record)

    In this case you can do a sql query that joins the two tables

    select Year, Name, Make, Skipper, count(hunts.id) from boats 
    join hunts on boats.id = hunts.BoatId
    group by Year, Name, Make, Skipper;
    

    What this will return is a certain boat with a total count of hunt records.

    Things you can research into:

    • MySQL relationships
    • MySQL joins
    • MySQL aggregates
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制