如何选择单个匹配记录(如果存在),否则选择默认值为“ 0”的所有记录?

时间:2019-10-16 13:44:31

标签: php mysql sql

SELECT baza WHERE owner='bako' else owner='0'

我希望看到所有尚未分配给所有者的工作的清单,但是一旦选择其中一项并刷新页面,我只会看到我选择的一项。

类似的东西:

SELECT baza WHERE owner='bako' else owner='0'

现在我有这个:

$sql = "SELECT * FROM baza WHERE owner='0' ";

但是我想要这样:

$username = $_SESSION['username']; 
$sql = "SELECT * FROM baza WHERE owner='$username' else owner='0'"; 

这是我的PHP

<?php
header("charset: UTF-8");
$conn = mysqli_connect("localhost", "root", "", "dam");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$username = $_SESSION['username'];


$sql = "SELECT * FROM `baza`  WHERE owner='0' ";


mysqli_set_charset($conn, "utf8");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row


    while($row = $result->fetch_assoc()) {
        echo 

        "
         <br><br><br> <br><br>
        <div id='names_bord'> 

        <div id='names_name'>  name: " . $row["saxeli"] . " </div>
        <div id='names_price'>  price: " . $row["tanxa"] . " </div>
        <div id='names_a'>     city: " . $row["qalaqi"]. " " . $row["misamarti"]. "</div>
        <div maxlength='10' id='names_a'>   comment: " . $row["komentari"]. " </div>

        <div id='names_accept'><a href=\"detail - Copy.php?id=".$row['id']."\"><img src='infoicon.png' ></i></a></div>

         </div>

        ";

    }

} else {
    echo "0 results"; 
}

1 个答案:

答案 0 :(得分:0)

所以,我解决了这个问题,现在,根据经验丰富的人士的说法,我想知道它的正确性

<?php
header("charset: UTF-8");
$conn = mysqli_connect("localhost", "root", "", "dam");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// firs if it is
$username = $_SESSION['username'];
$sql = "SELECT * FROM `baza` WHERE owner='$username' && kategoria='' ";

mysqli_set_charset($conn, "utf8");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row

while($row = $result->fetch_assoc()) {
echo "
<br><br><br> <br><br>
<div id='names_bord'> 
<div id='names_name'>  სახელი: " . $row["saxeli"] . " </div>
<div id='names_price'>  თანხა: " . $row["tanxa"] . " </div>
<div id='names_a'>     ქალაქი: " . $row["qalaqi"]. " " . $row["misamarti"]. "</div>
<div maxlength='10' id='names_a'>   კომენტარი: " . $row["komentari"]. " </div>
<div id='names_accept'><a href=\"detail - Copy.php?id=".$row['id']."\"><img src='infoicon.png' ></i></a></div>
</div>";}

} else { 
// else if it is
$sqll = "SELECT * FROM `baza` WHERE owner='0' && kategoria='' ";

mysqli_set_charset($conn, "utf8");
$result = $conn->query($sqll);
if ($result->num_rows > 0) {
// output data of each row

while($row = $result->fetch_assoc()) {
echo "
<br><br><br> <br><br>
<div id='names_bord'> 
<div id='names_name'>  სახელი: " . $row["saxeli"] . " </div>
<div id='names_price'>  თანხა: " . $row["tanxa"] . " </div>
<div id='names_a'>     ქალაქი: " . $row["qalaqi"]. " " . $row["misamarti"]. "</div>
<div maxlength='10' id='names_a'>   კომენტარი: " . $row["komentari"]. " </div>
<div id='names_accept'><a href=\"detail - Copy.php?id=".$row['id']."\"><img src='infoicon.png' ></i></a></div>
</div>";}

 }
}
$conn->close();
?>