lyax Posted May 14, 2007 Share Posted May 14, 2007 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??? Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/ Share on other sites More sharing options...
suttercain Posted May 14, 2007 Share Posted May 14, 2007 You should be able to use the variable in the sentence, as you wrote above if you use double quotes. "Wow that is so $var up!" or in single using . 'Wow that is so ' . $var . ' up!' Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252805 Share on other sites More sharing options...
marf Posted May 14, 2007 Share Posted May 14, 2007 I was about to say the same thing. Also it sounds like from what you wrote you have code. Post the code and we can help you fix it that much faster. Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252807 Share on other sites More sharing options...
lyax Posted May 14, 2007 Author Share Posted May 14, 2007 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 . Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252846 Share on other sites More sharing options...
suttercain Posted May 14, 2007 Share Posted May 14, 2007 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"; Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252859 Share on other sites More sharing options...
lyax Posted May 14, 2007 Author Share Posted May 14, 2007 $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 .. Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252884 Share on other sites More sharing options...
chigley Posted May 14, 2007 Share Posted May 14, 2007 Put a $ before row Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252885 Share on other sites More sharing options...
kenrbnsn Posted May 14, 2007 Share Posted May 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252888 Share on other sites More sharing options...
lyax Posted May 14, 2007 Author Share Posted May 14, 2007 yes that gives no error, thats the answer i think. thanks so much a lot. Quote Link to comment https://forums.phpfreaks.com/topic/51332-solved-if-else-and-mysql/#findComment-252907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.