Help with queries needed

Discussion in 'PHP' started by crazyryan, Feb 1, 2009.

  1. #1
    Hey

    Having a little dilemma in doing this the most efficient way.

    Basically, I have 2 properties in the database and then weekendEligible is set to 1 on both of them.

    I want to assign the properties to a user.

    If a property is assigned on Saturday, it can no longer be assigned on Saturday for another user but it can be assigned on Sunday, vice versa.

    Right now I have this:
    http://pastebin.com/m6aab321d

    I've run the following on 2 users, so it should insert a total of 4 rows in the assignments table.
    
    $this->createAssignment($_SESSION['user_id'], 6);
    $this->createAssignment($_SESSION['user_id'], 7);
    PHP:
    However, when I assigned properties I can only get 3 combinations in the database, for some reason it won't assign property_id 1 to user_id 1 on day 6 (saturday)

    Does anyone have any suggestions on how I can fix it or a better solution to the problem I have. Thanks :)

    (Willing to give a little reward via PayPal for anyone who can come up with a working solution)
     
    crazyryan, Feb 1, 2009 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not entirely sure what you're trying to do but just from looking at the code, it looks like you might be overwriting your first $query result before you've finished with it.

    // Original query
    $query = mysql_query(sprintf("SELECT * FROM `properties` WHERE properties.weekendEligible = '%s' ORDER BY RAND()", 1));
    
    // ...
    
    // The first time this runs, it'll be with the $query resource you're expecting
    // from the original query above
    while($row = mysql_fetch_object($query))
    {
       // But then you give $query a new result resource here so the next iteration
       // of this while() loop will give $row the result of THIS query
       $query = mysql_query(sprintf("SELECT `assignment_id` FROM `assignments` WHERE `day` = '%s' AND `property_id` = '%s'", $day, $row->property_id));
       // ...
    }
    PHP:
    Unless that's intentional, you might want to change the query within the while() loop to use a different variable.
     
    rodney88, Feb 1, 2009 IP