drkbpwk609294 2016-02-02 00:38
浏览 13
已采纳

PHP中的布尔条件[重复]

This question already has an answer here:

Hello I'm new to php and mySQL. Basically I have a table of users and I want to echo the id, name and surname of the users if their "valid" integer is equal to 1, here is my attempt;

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

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

$sql = "SELECT ID, Name, Surname, Valid FROM myTable";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
while($row = $result->fetch_assoc()) {
        if($row["Valid"] = 1) {echo "id: " . $row["ID"]. " - Name: " . $row["Name"]. " " . $row["Surname"]. "<br>";
    }
}
} else {
echo "0 results";
}
$conn->close();
?>

Using this code gives me a list of all the users, even though their valid integers are zero.

Thanks,

</div>
  • 写回答

2条回答 默认 最新

  • dongxu1668 2016-02-02 00:42
    关注

    In PHP to check for conditions as you are you need the == operator instead of the = operator. All you are doing is setting all the valid columns to 1.

    if($row["Valid"] == 1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题