Reputation:
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!
<?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
Reputation: 98921
if
inside the while
loop$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
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