If statement not working

Problems with $row ??

Page 1 of 1

3 Replies - 618 Views - Last Post: 27 August 2008 - 09:54 AM Rate Topic: -----

#1 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

If statement not working

Posted 27 August 2008 - 09:33 AM

Hello,

I'm practicing PHP & mysql by making a small text based game (what else?). In my table 'users' there's a column called 'state' which is basically the player's type of government. I have a page called 'soldiers.php' where the player trains soldiers, if they have a government set to 'Police State' then the cost is 2.5 per soldier, otherwise, it's 3.

I made this code, to me it looks correct, but i've noticed it is always set to 2.5, regardless of what value $row['state'] is:

<?php 
$user = $_SESSION['username']; // session for their username
$result = mysql_query("SELECT * FROM users WHERE username='$user'") or die(mysql_error());  
$row = mysql_fetch_array($result);

$money = $row['money']; // how much money they have
$state = $row['state']; // what government they have
if ($state = 'Police State') {
$cost = 2.5; // reduced cost for specific government
	} else {
$cost = 3; // normal cost for everything else
}
echo '<a class="style1">You have '.$money.' dinars. It costs '.$cost.' dinars per soldier.'; // cost is always displayed as 2.5 for some reason
?>



Thank you

EDIT: did a bit of thinking - do if statements not work on setting values for variables?

This post has been edited by Sayid Ahmed: 27 August 2008 - 09:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: If statement not working

#2 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: If statement not working

Posted 27 August 2008 - 09:47 AM

you need to use ==, not =

you're setting $row equal to Police State, not checking it...

This post has been edited by JBrace1990: 27 August 2008 - 09:48 AM

Was This Post Helpful? 1
  • +
  • -

#3 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Re: If statement not working

Posted 27 August 2008 - 09:52 AM

oh dear is that it? God, i feel schoopid now

thanks
Was This Post Helpful? 0
  • +
  • -

#4 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: If statement not working

Posted 27 August 2008 - 09:54 AM

lol... we all do it sooner or later, don't worry =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1