user5889300
user5889300

Reputation:

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. 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". Neither of my bottom 2 'if' statements work. What am I doing wrong? Thanks! 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);

?>

Upvotes: 1

Views: 2250

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98921

  1. Put the if inside the while loop
  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), i.e. :

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

Upvotes: 2

Stian Skjelstad
Stian Skjelstad

Reputation: 2335

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

Index is a text.

Upvotes: -1

Julie Pelletier
Julie Pelletier

Reputation: 1716

Your main problem is that you put your conditions after there are no more records returned. Move them inside your while loop.

Note that you should add a second parameter to mysqli_fetch_array(): MYSQLI_ASSOC for it to return an associative array.

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

Upvotes: 1

Related Questions