Jump to content

Recommended Posts

Okay, here is the issue. I am running a basic 'wager-tracking' web service, that stores your wager in a database. The components of each wager are 'username, opponent, wager, date, id'. Users are kept under the following components 'username, name, password, email, fb_uid, email_hash' .

 

I have one table that loads correctly, by query'ing the database for all 'wagers' that are made by the current user. That code is here:

$bets = $user->getBetsFor();
if ($bets) {
  echo '<div id="showbets" class="bluebox">';
  $betsCount = 0;
  foreach ($bets as $bet) {
    $betsCount += 1;
  }

  echo '<h3>Recent Bets You Made</h3>';
  echo '<table>'
    .'<tr>'
    .'<th>Date</th>'
    .'<th>Opponent</th>'
    .'<th>Wager</th>'
    .'</tr>';
  foreach ($bets as $bet) {
    echo render_bet($bet);
  }
  echo '</table>';

  if ($betsCount > 0) {
    echo '<div style="padding: 5px; font-weight: bold;">You made ' . $betsCount . ' bets recently!</div>';
  }

  echo '</div>';
} else {
echo '<div id="showbets" class"bluebox">';
echo '<h3>Recent Bets You Made</h3>';
echo '<div style="padding: 5px; font-weight: bold;">You\'ve made no bets recently!</div>';
echo '</div>';
}

 

At the beginning it calls the following function from user.php

  function getBetsFor() {
    if ($this->bets === null) {
      $ret = queryf('SELECT * FROM bets WHERE username = %s ORDER BY date DESC LIMIT %d',
                    $this->username, MAX_DISPLAY_BETS);
      if (!$ret) {
        return null;
      }

      $bets = array();
      while ($row = mysql_fetch_assoc($ret)) {
        $bet = new Bet($this, $row);
        $bets[] = $bet;
      }
    }

    $this->bets = $bets;
    return $this->bets;
  }

 

 

Now, the issue is I need to duplicate this table, except display all bets that are against the current user. But to start simply duplicating the table is fine. Problem is (if I copy and paste the above code (1st code block) to right below it (in index.php, so it should load a new exactly the same table below the first one), the table doesn't load the bets!) After it query's the database it appears to fail the test if ($bets) and then goes to the else statement (displaying an empty table).

 

I don't see any reason why this should happen, if the first query runs, and passes if($bets), and the second time around it runs the SAME query, it should also pass if($bets). but it doesnt..

 

Any help is appreciated! let me know if you need any more information. Thanks!

 

P.S. I've tried to make this problem as simple as possible, I only need to be able to duplicate the table, the rest I can take care of.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/196302-php-and-mysql-query-issue/
Share on other sites

I'm pretty sure the problem is with the first conditional in the getBetsFor() function.

 

if ($this->bets === null) {
      $ret = queryf('SELECT * FROM bets WHERE username = %s ORDER BY date DESC LIMIT %d',
                    $this->username, MAX_DISPLAY_BETS);

 

If I am reading this right, you are only running the query if $this->bets has not been set. So, on the second call you make to that function $this->bets (which has scope in the class) has been set so the query doesn't run. Then, the only code that does run in that function is this

   $this->bets = $bets;
    return $this->bets;

$bets has scope only in the function, so on the second run it is not getting set from the query because of the initial conditional so it is a null value. So, the end of the query replaces the previous value of $this->bets with a null value.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.