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
  • 607
  • 2
  • 9
  • 30

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
  • 607
  • 2
  • 9
  • 30
  • so you got the solution.isn't it? – Anant Kumar Singh Jun 04 '15 at 18:53
  • Yeah I managed to figure it out :) – rshah Jun 04 '15 at 19:03
  • Can i gave you explanation why you need to do that and why i posted below answer? – Anant Kumar Singh Jun 04 '15 at 19:06
  • `print_r` prints a human-readable description about the variable. That is not what I needed to do, I needed to get the variable out of the array. Because I was only inserting multiple columns into a single object reference, it would only return the empty array. Therefore I needed to fetch all the columns into an associative array where each column name refers to a specific value. – rshah Jun 04 '15 at 19:09
  • great you know all. but by seeing my code you won't get that. thanks. – Anant Kumar Singh Jun 04 '15 at 19:13