1

I'm trying to write the output of a MySQL select into a variable. The problem is that i get the error "Array to String conversion".

    $user = mysql_query("select username from user");
echo "<table border='1'>";
if (isset($_POST['winneron'])) {
    echo "<tr>";
    while ($printuser = mysql_fetch_array($user)) {
        echo "<th align='center'>". $printuser['username'] . "</th>";
    }
    echo "</tr>";
    $user = mysql_query("select username from user");
    while ($printuser = mysql_fetch_array($user)) {
        $games = mysql_query("SELECT s.spielid, date, team1, team2, sieger, wettid, u.userid, w.spielid, team 
                              FROM user u, spiele s, wette w 
                              WHERE u.userid = w.userid 
                              AND u.username = '$printuser' 
                              AND w.spielid = s.spielid"); <-- Error line
        while ($printgames = mysql_fetch_array($games)) {
            if ( $printgames['sieger'] == $printgames['team1'] ) {
                echo "<tr><td align='center'><b>". strtoupper($printgames['sieger']) . "</b></td></tr>";    
            }
            else { 
                echo "<tr><td align='center'>". strtoupper($printgames['sieger']) . "</td></tr>"; 
            }
        }
    }
}
3
  • on which line? note that if you try to fetch from the same $user twice, next time you might get an empty array Commented Feb 20, 2014 at 13:25
  • Edited the code so its easier to read. Already thought that $user could be empty after the first fetch, so i added it again after the while loop. Commented Feb 20, 2014 at 13:26
  • 1
    You must understand that you're selecting multiple columns so you can't compare the username with an entire array of values. You must determine which index of the array, i.e. the username index, you wish to compare in your search query. Use prepared statements and bind values into variables, otherwise the code gets messy and will not work, but it's easy to avoid. Commented Feb 20, 2014 at 13:31

2 Answers 2

1

AND u.username = '$printuser' replace it with AND u.username = '".$printuser['username']."'

Sign up to request clarification or add additional context in comments.

Comments

1

$printuser is a array and you are using it like a string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.