php - 使用 PHP 比较数据库中的值

标签 php mysql database

我想知道如何将数据库中的值/varchar 与我自己的 PHP 脚本中的字符串进行比较。下面是我的数据库的图片,如果它有帮助的话,我只想将 ThunderOrNot 列 (ID = 1) 中的值与字符串“Thunder”进行比较。我的底部 2 个“if”语句都不起作用。我究竟做错了什么?谢谢! Picture of DB

    <?php

$link = mysqli_connect('.....', '.....', '.....', '.....');

$query = "select * from thunderDemo";
$result = mysqli_query($link, $query);

while($row = mysqli_fetch_array($result))
{
         echo $row["ThunderOrNot"];
}

if($row[ThunderOrNot] == 'Thunder')
{
         echo "The Value equals Thunder";
}

if($row == 'Thunder')
{
         echo "The Value equals Thunder";
}

mysqli_close($link);

?>

最佳答案

  1. if放在while循环中
  2. $row["ThunderOrNot"] 添加引号(不那么重要,因为 未加引号的字符串 将被解释为 常量 php,在这种情况下,它的值将是 ThunderOrNot(荣誉:Jay Blanchard),即:

while($row = mysqli_fetch_array($result))
{
    if($row["ThunderOrNot"] == 'Thunder'){
        echo "The Value equals Thunder";
    }
}

关于php - 使用 PHP 比较数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37033253/

相关文章:

MySQL COUNT()、SUM() 和 GROUP BY

MySQL 报告 CONCAT 不存在

MySQL用父ID替换父名称

php - 是否可以使用 PHP 更改 HTML 元素值

php - php 中格式错误的 unicode/十六进制

mysql - 在 NodeJS 中 Sequelize : Inner JOIN implementation Failure

c# - 将 Entity Framework 与自定义子类一起使用

php - MYSQL PHP 单数据库应用性能

php - utf8_general_ci 数据应该如何存储?

php - Elasticsearch 中的多字段搜索无效?