Jump to content

[SOLVED] if else and mysql


lyax

Recommended Posts

i want to check my mysql database for the row if it is 0 or 1 or whatever it is and then print a word/words into a sentence according to the result..

for ex: my TABLE has a row named HEALTH and its value is 3. i want to use a variable in a sentences that will print GOOD. but if it were 2, script would print NOT BAD and for 1, print BAD.. my sentences is like this: Hi bla bla, your health is $var now.. how can i do this.. with if else tag i can make it print a sentences but cant do as a $var in a sentence???

Link to comment
https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/
Share on other sites

after connected to the database, the code must be something like this : (gives parse error, couldnt do that)

 

if (row['health']=="3")

  $var = 'good';

elseif (row['health']=="2")

  $var = 'normal';

elseif (row['health']=="1")

  $var = 'bad';

else

  echo "";

 

 

your health is $var .

 

 

Can you post the error?

 

I think the error is going to tell you that you need brackets:

 

if (row['health']=="3") {
  $var = 'good'; }
elseif (row['health']=="2") {
  $var = 'normal'; }
elseif (row['health']=="1") {
  $var = 'bad'; }
else {
  echo ""; }

echo "Your health is $var";

$result = mysql_query("SELECT * FROM utopia
WHERE kullaniciadi='$session->username'");

if (row['disgorunus']=="3") {
  $var = 'good'; }
elseif (row['disgorunus']=="2") {
  $var = 'normal'; }
elseif (row['disgorunus']=="1") {
  $var = 'bad'; }
else {
  echo ""; }

echo "Your health is $var";  //this is not here originally.

while($row = mysql_fetch_array($result))
  {
  echo $row['karakteradi'] . " şu an " . $row['disgorunus'] . " görünüyor ve kendini " . $row['ruhhali']. " hissediyor. " . $row['karizma']. " karizmaya sahip ve sağlığı " . $row['saglik']. ".";
  }

 

this is the code i am using.

gives parse error for the line if (row['disgorunus']=="3") { says: parse error on this line ..

 

Also, you do the query, but you never fetch the row information. Try something like this:

<?php
$result = mysql_query("SELECT * FROM utopia WHERE kullaniciadi='$session->username'");
$row = mysql_fetch_assoc($result);
switch ($row['disgorunus']) {
     case '1':
          $var = 'bad';
          break;
     case '2':
          $var = 'normal';
          break;
     case '3':
          $var = 'good';
          break;
      default:
          $var = '';
}
echo "Your health is $var";  //this is not here originally.
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.