Jump to content

ELSE IF Issue


Usarjjaco

Recommended Posts

Hello Everyone; Here is the code I have that I'm trying to make function properly:

$trainings=("SELECT tp FROM ranchers WHERE username ='$uname'");$horsetrained=("SELECT trained FROM horsedetails WHERE username ='$uname' AND stall =1");if ($horsetrained='Y')echo "Horse Has Already Been Trained Today! Please Click Your Browsers Return Button!";else if ($trainings=0) echo "Not Enough Training Sessions!, Please Click Your Browsers Back Button!";else  {echo "Horse Has Been Trained!";echo "<br>";echo "<br>";echo "<a href='www.virtua-rancher.com/myranch.php'>Return Home</a>";mysql_query("UPDATE ranchers SET tp=tp - 1 WHERE username ='$uname'");mysql_query("UPDATE horsedetails SET trained=Y WHERE username ='$uname' AND stall=1");}

The issue I'm having is that the $horsetrained variable isn't working properly.When the code executes it should deduct 1 from the Training Sessions (Which It Does)It Should also change the Trained value from 'N' to 'Y' (Which it does when I eliminate the $horsetrained variable.)But the problem is it doesn't read the mysql database properly and therefore even if the $horsetrained='N' it will give me the error echo. I'm guessing it's something to do with my code here:

$horsetrained=("SELECT trained FROM horsedetails WHERE username ='$uname' AND stall =1");

Any help would be appreciated... just trying to make it so that if conditions A & B are not true; then it will execute; if A is true it will give error A; if B is true it will give error B.

Link to comment
Share on other sites

But the problem is it doesn't read the mysql database properly
No, the problem is that you're not writing the code properly. Doing this:$horsetrained=("SELECT trained FROM horsedetails WHERE username ='$uname' AND stall =1");does nothing with the database. All you did there is build a string. You didn't send a query to the database, you just made a string. If you actually want to send that to the database you need to use mysql_query to get the result, and then use something like mysql_fetch_assoc to get the rows from the result. Just building a string doesn't automatically send it to the database and get the result back.Also, this is not the correct way to check the value of a variable:if ($horsetrained='Y')The = operator sets a value, it does not test. After doing that, $horsetrained will be set to "Y", you aren't testing whether or not it's set to "Y". If you want to test the value you need to use either == or ===, not =.
Link to comment
Share on other sites

No, the problem is that you're not writing the code properly. Doing this:$horsetrained=("SELECT trained FROM horsedetails WHERE username ='$uname' AND stall =1");does nothing with the database. All you did there is build a string. You didn't send a query to the database, you just made a string. If you actually want to send that to the database you need to use mysql_query to get the result, and then use something like mysql_fetch_assoc to get the rows from the result. Just building a string doesn't automatically send it to the database and get the result back.Also, this is not the correct way to check the value of a variable:if ($horsetrained='Y')The = operator sets a value, it does not test. After doing that, $horsetrained will be set to "Y", you aren't testing whether or not it's set to "Y". If you want to test the value you need to use either == or ===, not =.
Ok I believe I'm following everything you said ; and I mis-spoke when I said it wasn't working right.. coding is always user error :) . The question I have is would I set the result up as an array if it's only going to be 1 row every time? Because both variable A and B are coming from 1 row. Thanks again :)
Link to comment
Share on other sites

Any response from MySQL always comes back as a MySQL result resource. There's not a difference between a result with 0 rows or 1 row or more than 1, they're all the same structure. PHP doesn't know you only want one row, so it always returns the same type of resource. You would just do something like this to check for errors:

$result = mysql_query($query);$row = mysql_fetch_assoc($result);if (!$row){  echo 'No data returned';}

After that $row would be your row of data where you can access each of the fields.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...