-3

If I want to use this code twice or more on a page (with slight modifications), I get the following error message:

"Call to a member function fetch_assoc() on a non-object"

The line of code which is causing the issues is this:

while(($array_results[] = $results->fetch_assoc()) || array_pop($array_results));

The code is full is as follows:

<?PHP
if($_SESSION['member_unique_id']=="supermember"){
    $results = $mysqli->query("SELECT client_organisation_name, client_id FROM clients ORDER BY client_organisation_name ASC");
  }
 else{
    $results = $mysqli->query("SELECT client_organisation_name, client_id FROM clients WHERE member_unique_id='".$_SESSION['member_unique_id']."' ORDER BY client_organisation_name ASC");
    }
    while(($array_results[] = $results->fetch_assoc()) || array_pop($array_results));
    echo '<div class="dashDropdown">'."\n";
    foreach($array_results as $client) {
        echo '<a href="create-a-property-2.php?client='.$client['client_id'].'" class="dashPropertyClientHover">'.$client['client_organisation_name'].'</a>'."\n";
    }
    echo '</div>'."\n";
    $results->close();
$mysqli->close();
?>

How can this be amended to prevent the error message appearing and for this code (modified slightly) to appear more than once in my source code? Any help as always is appreciated.

darylsummers
  • 89
  • 1
  • 10
  • None of these help me understand my error as they are not explained in depth – darylsummers Nov 10 '15 at 18:15
  • Session has been started yes, if I play around with "while(($array_results[] = $results->fetch_assoc()) || array_pop($array_results));" the list populates but still throws the same error, so I am unsure how to amend this – darylsummers Nov 10 '15 at 18:30

1 Answers1

0
<?PHP
if($_SESSION['member_unique_id']=="supermember")
{
    $results = $mysqli->query("SELECT client_organisation_name, client_id FROM clients ORDER BY client_organisation_name ASC");
}
else
{
    $results = $mysqli->query("SELECT client_organisation_name, client_id FROM clients 
                                WHERE member_unique_id='".$_SESSION['member_unique_id']."' 
                                ORDER BY client_organisation_name ASC");
}
while($array_results = $results->fetch_assoc()) 
{
    $client_id=$array_results['client_id'];
    $client_org_name=$array_results['client_organisation_name'];
    ?>
    <div class='dashDropdown'>
        <a href="create-a-property-2.php?client=<?php echo $client_id;?>" class="dashPropertyClientHover">
            <?php echo $client_org_name;?>
        </a>
        <?php echo "<br>";?>
    </div>
    <?php echo "<br>";?>
<?}
$results->close();
$mysqli->close();
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77