简体   繁体   English

使用PHP比较数据库中的值

[英]Comparing values in a database with PHP

I was wondering how I could compare a value/varchar in my database to a string in my own PHP script. 我想知道如何将数据库中的值/ varchar与自己的PHP脚本中的字符串进行比较。 Below is a picture of my database if it helps and I just want to compare the value inside the ThunderOrNot column (ID = 1) to a string of "Thunder". 下面是我的数据库的图片(如果有帮助的话),我只想将ThunderOrNot列(ID = 1)内的值与字符串“ Thunder”进行比较。 Neither of my bottom 2 'if' statements work. 我最底层的2个“如果”陈述均无效。 What am I doing wrong? 我究竟做错了什么? Thanks! 谢谢! 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. Put the if inside the while loop if放入while循环中
  2. Add quotes to $row["ThunderOrNot"] (not as important, because an unquoted string will be interpreted as a constant by php and, in this case, its value will be ThunderOrNot (kudos: Jay Blanchard ), ie : $row["ThunderOrNot"]添加引号(不那么重要,因为未引号的字符串将被php解释为constant ,并且在这种情况下,其值将为ThunderOrNot (名称: Jay Blanchard ),即:

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

Your main problem is that you put your conditions after there are no more records returned. 您的主要问题是您在没有更多记录返回后放置条件。 Move them inside your while loop. 将它们移到while循环中。

Note that you should add a second parameter to mysqli_fetch_array() : MYSQLI_ASSOC for it to return an associative array. 注意,您应该在mysqli_fetch_array()MYSQLI_ASSOC添加第二个参数,以使其返回关联数组。

The condition would then be: if ($row['ThunderOrNot'] == 'Thunder') 条件将是: if ($row['ThunderOrNot'] == 'Thunder')

if($row["ThunderOrNot"] == 'Thunder')

索引是文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM