PHP: "expects parameter 1 to be mysqli_result"

I keep getting this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/content/e/q/u/equianadmin/html/htdocs/includes/footer.html

For this code:

 //fetch money
 $q = "SELECT money AS money FROM users WHERE username='{$_SESSION['username']}'";  
 $r = @mysqli_query ($dbc, $q); // Run the query.
 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
        $money=$row['money'];
 }

The code is on my footer.html include. The weird thing is, I don’t get it on certain pages. For instance, on a page with nothing on it, it works fine. But on a page with a ton of php code I get it. I also get this code sometimes on other pages. I don’t know whats going on! What can I do to fix it? Thanks!

Also, $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); is defined in my connection file.

P.S. Here’s an example of a page when I get the error:

<?php //for_sale.php
$page_title = 'View Horse for Sale';
include ('../includes/header.html');
echo '<h1>Horses For Sale</h1>';
require_once ('../../mysqli_connect.php');
// Number of records to show per page:
$display = 10;
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
 $pages = $_GET['p'];
} else { // Need to determine.
  // Count the number of records:
 $q = "SELECT COUNT(horse_name) FROM horses WHERE for_sale='1'";
 $r = @mysqli_query ($dbc, $q);
 $row = @mysqli_fetch_array ($r, MYSQLI_NUM);
 $records = $row[0];
 // Calculate the number of pages...
 if ($records > $display) { // More than 1 page.
  $pages = ceil ($records/$display);
 } else {
  $pages = 1;
 }
} // End of p IF.
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
 $start = $_GET['s'];
} else {
 $start = 0;
}
// Determine the sort...
// Default is by registration date.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';
/*// Determine the sorting order:
switch ($sort) {
 case 'ln':
  $order_by = 'last_name ASC';
  break;
 case 'fn':
  $order_by = 'first_name ASC';
  break;
 case 'rd':
  $order_by = 'registration_date ASC';
  break;
 default:
  $order_by = 'registration_date ASC';
  $sort = 'rd';
  break;
}*/
 
// Make the query:
$q = "SELECT horse_name, user_name, horse_id, price FROM horses WHERE for_sale='1' ORDER BY price LIMIT $start, $display";  
$r = @mysqli_query ($dbc, $q); // Run the query.
// Table header:
echo '<table align="center" cellspacing="0" cellpadding="5" width="75%">
<tr>
 <td align="left"><b>Horse Name</b></td>
 <td align="left"><b>User Name</b></td>
 <td align="left"><b>Price</b></td>
 <td align="left"><b>Buy</b></td>
 
</tr>
';
// Fetch and print all the records....
$bg = '#eeeeee'; 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
  echo '<tr bgcolor="' . $bg . '">
  <td align="left">' . $row['horse_name'] . '</td>
  <td align="left">' . $row['user_name'] . '</td>
  <td align="left">' . $row['price'] . '</td>
  <td align="left"><a href="buy_horse.php?id=' . $row['horse_id'] . '">Buy</a></td>
 </tr>
 ';
} // End of WHILE loop.
echo '</table>';
mysqli_free_result ($r);
mysqli_close($dbc);
// Make the links to other pages, if necessary.
if ($pages > 1) {
 
 echo '<br /><p>';
 $current_page = ($start/$display) + 1;
 
 // If it's not the first page, make a Previous button:
 if ($current_page != 1) {
  echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&sort=' . $sort . '">Previous</a> ';
 }
 
 // Make all the numbered pages:
 for ($i = 1; $i <= $pages; $i++) {
  if ($i != $current_page) {
   echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';
  } else {
   echo $i . ' ';
  }
 } // End of FOR loop.
 
 // If it's not the last page, make a Next button:
 if ($current_page != $pages) {
  echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>';
 }
 
 echo '</p>'; // Close the paragraph.
 
} // End of links section.
 
include ('../includes/footer.html');
?>

Thanks in advance!