0

I'm working on a system that will be able to tell the user if the class they are trying to sign up for is full. So I fetch the record from my database using the snippet below where $camp is the $camp they choose and $conn is the database connection. I know that part is working correctly as I've been using it for other pieces.

$full = false;
function CheckFull($conn,$camp)
{
    //construct SQL string to get desired record
    $sql = "SELECT * FROM camps WHERE name='" . $camp . "' AND archive='0'";
    //SQL query and fetching array to grab variables
    $result = mysqli_query($conn, $sql);
    $event = mysqli_fetch_array($result);
    $max = $event['maxparticipant'];
    $current = $event['participants'];

    //Check Conditions for Fullness
    if($max<=$current)
    {
        $full = true;
    }
    else
    {
        $full = false;
    }

}

I've fetched many records like this before and had them echo on the page they need to display on but when comparing these two variables from the desired row I can never get it to return true even though in my database maxparticipant is < participants. Both columns are structured for integers. I'm wondering if because they are fetched from a database if there is some type issues going here?

Dharman
  • 30,962
  • 25
  • 85
  • 135
BlueLycan
  • 31
  • 8
  • It should work. Are you sure they're integers in the DB, not VARCHAR? – Barmar Jan 16 '20 at 22:35
  • Your function never returns anything – Dharman Jan 16 '20 at 22:35
  • If you want to update the global `$full` variable you need to put `global $full;` at the beginning. – Barmar Jan 16 '20 at 22:36
  • 1
    But it would be better to use `return $full;` and assign the result in the caller. – Barmar Jan 16 '20 at 22:36
  • If you want to update the global `$full` your function must return a value which you should then assign to this variable. – Dharman Jan 16 '20 at 22:36
  • Hello Dharman, thanks for the warning! I'll be sure to read up more on that but for now I'm more or less just learning the ins and outs of stuff. What I meant to say is I never seem to enter my if statement. To overwrite $full as True. @Barmar I've triple checked the types and they are just of type int. – BlueLycan Jan 16 '20 at 22:38
  • How do you know it's not entering the `if` statement? – Barmar Jan 16 '20 at 22:39
  • Well I suppose that is a good point. I've been testing it by having an if statement that echos true or false based on what $full is after CheckFull is called. I was assuming that because $full was never changed that i had never entered the if statement. – BlueLycan Jan 16 '20 at 22:42
  • Awesome! I checked the link @Barmar gave to giving my function access to outside variables and was able to figure out my problem! Thank you everyone. Also Dharman thank you for the insight I will be looking into this soon. – BlueLycan Jan 16 '20 at 22:50

0 Answers0