0

It says I'm getting these errors:

Notice: Array to string conversion in C:\xampp\htdocs\Team_Aveera_Website\inc\user_functions.php on line 19 Array Notice: Array to string conversion in C:\xampp\htdocs\Team_Aveera_Website\inc\user_functions.php on line 22

When I try to index my array. What I'm doing is getting all rows from a database and adding a value from the row to an array. Then I get a random object from the array:

<?php
    function getRandomAdminStream($db) {
        try {
            $twitchNames[] = array();

            $SQL = $db->prepare('SELECT * FROM users');
            $SQL->setFetchMode(PDO::FETCH_ASSOC);
            $SQL->execute();

            $i = 0;
            while($row = $SQL->fetch() !== FALSE) {
                if($row['rank'] == 1) {
                    $twitchNames[$i] = $row['twitchUsername'];
                    $i++;
                }
            }

            $random = $twitchNames[rand(0, count($twitchNames) - 1)];
            echo $random;

            echo '<iframe id="home-stream" type="text/html" height="420"
                src="http://www.twitch.tv/'.$random.'/embed"
                frameborder="0"></iframe>
                <div class="info-holder">
                </div>';
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
?>
rshah
  • 675
  • 2
  • 12
  • 32

1 Answers1

0

Even though I set the fetch mode:

$SQL->setFetchMode(PDO::FETCH_ASSOC);

It still wouldnt use an associative array in the while loop, so I had to change the statement like so:

while($row = $SQL->fetch(PDO::FETCH_ASSOC)) {}
rshah
  • 675
  • 2
  • 12
  • 32