簡體   English   中英

如何在 sql 和 php 中執行 if-else 條件?

[英]How to do an if-else condition in sql and php?

我想做的是一個 select 查詢,然后如果它是真的設置一個名為'afiliadospadron'的表的變量為1,但如果它是假的(它不返回任何東西)只需將變量設置為0。我的代碼是'工作。 有人可以幫我嗎?

$query= "select nrodoc from oct20 where nrodoc = 05463xx union select nrodoc from dic20 where nrodoc = 054631xx";
$query2= "update afiliadospadron set condVotar = '1' where nroDoc = '5463xxx' ";
//$query3= "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463xxx'";
if ($query) {
    $query2;
    } else {
        echo "next time";
//    $query3 = "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463192'";;
    }
$statement = $conexion->prepare($query);
$statement = $conexion->prepare($query2);
//$statement = $conexion->prepare($query3);
$statement->execute();

試試看

if ($result = $mysqli -> query($query)) {
  $query2;
  // Free result set
  $result -> free_result();
}

代碼來自: https://www.w3schools.com/php/func_mysqli_query.asp

答案是

$statement = $conexion->prepare($query);
$statement->execute();
$result = $statement->fetchAll();

if ($result = $result ) {
    echo "yas";
    $statement = $conexion ->prepare($query2);
    $statement->execute();
    // Free result set
 
} else {
    echo "Wasnt found";
    $statement = $conexion ->prepare($query3);
    $statement->execute();
} 

暫無
暫無

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

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