簡體   English   中英

如何在 mysql 中查詢兩個表以獲取過濾結果

[英]How do I query two tables for a filtered result in mysql

我有一個帶有建築物(8052monticello)的表和一個帶有辦公室(individualspecs)的單獨表,單擊建築物頁面中的相應建築物鏈接后,我通過對處理頁面 unit_specs.php 的 get 請求將建築物 ID 發送到處理頁面 unit_specs.php,但是 unit_specs正在向我顯示表中的所有單位,但是我只希望 unit_specs.php 顯示與其各自建築物對應的單位。 我怎樣才能解決這個問題?

 $querystats='SELECT individualspecs.Space, individualspecs.Size, 
 individualspecs.Price, individualspecs.Id';
 $querystats.= ' FROM individualspecs'; 
 $querystats.= ' INNER JOIN 8052monticello ON 
 individualspecs.fk_Id=8052monticello.id';

if ($r = mysqli_query($connection, $querystats)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_assoc($r)) {

if ($row['Price']){

    print "<p><h3> Unit # {$row['Space']}</h3>  
    {$row['Size']} Sq Feet<br>
    {$row['Price']} Monthly rent<br>
    <a href=\"edit_UNIT.php?Id={$row['Id']}\">Edit</a>
    <a href=\"delete_UNIT.php?Id={$row['Id']}\">Delete</a>
    </p><hr>\n";
}
}
} 

單位表

建築物表

這是完整的代碼:

 <!doctype html>

 <html lang="en">

 <head>
 <style>
.container{
    padding: 1em;
}

</style>
<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" 
integrity="sha384- 
Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" 
crossorigin="anonymous">
<meta charset="utf-8">

<title>View the units @ </title>

</head>
<body>

<div class="container">
<h1>Units</h1><hr>

<?php // Script 12.6 - view_BUILDINGS.php


$connection = mysqli_connect('localhost', 'user', 'password', 
'BUILDINGS');




// Define the query but only for info:


$querystats='SELECT individualspecs.Space, individualspecs.Size, 
individualspecs.Price, individualspecs.Id';
$querystats.= ' FROM individualspecs'; 
$querystats.= ' WHERE individualspecs.fk_building= 8052monticello.UNIT';

if ($r = mysqli_query($connection, $querystats)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_assoc($r)) {

if ($row['Price']){

    print "<p><h3> Unit # {$row['Space']}</h3>  
    {$row['Size']} Sq Feet<br>
    {$row['Price']} Monthly rent<br>
    <a href=\"edit_UNIT.php?Id={$row['Id']}\">Edit</a>
    <a href=\"delete_UNIT.php?Id={$row['Id']}\">Delete</a>
    </p><hr>\n";
}
}
} 


//end of get image query
else { // Query didn't run.

print '<p style="color: red;">Could not retrieve the data because:<br>' . 
mysqli_error($connection) . '.</p><p>The query being run was: ' . 
$querystats . '</p>';

} // End of query IF.



mysqli_close($connection); // Close the connection.



?>
</div>

</body>

</html>

這里有幾件事......

  1. 你沒有使用8052monticello任何東西,所以沒有理由加入
  2. 假設您的請求類似於/unit_specs.php?id=123 ,您需要將individualspecs.fk_Id (您的建築物 ID)與$_GET['id'] 最好的方法是使用准備好的語句並綁定參數。

例如

$buildingId = $_GET['id'] ?? null; // PHP 7 syntax, use "$buildingId = isset($_GET['id']) ? $_GET['id'] : null;" for older versions
$stmt = $connection->prepare(
        'SELECT Space, Size, Price, Id FROM individualspecs WHERE Price > 0 AND fk_Id = ?');
$stmt->bind_param('i', $buildingId); // bind the first parameter as an integer
$stmt->execute();
// I just find bind_result() easier to work with, YMMV
$stmt->bind_result($space, $size, $price, $id); 
// PHP's alternative syntax makes for better readability IMO
while ($stmt->fetch()) : ?>
  <p>
    <h3> Unit # <?= $space ?></h3> <!-- FYI: H3 should not be in a P tag -->
    <?= $size ?> Sq Feet<br>
    <?= $price ?> Monthly rent<br>
    <a href="edit_UNIT.php?Id=<?= $id ?>">Edit</a>
    <a href="delete_UNIT.php?Id=<?= $id ?>">Delete</a> <!-- deleting via a GET request is a bad idea -->
  </p>
  <hr>
<?php endwhile;

有用的鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM