0

I keep seeing this error on the line containing the fetch Values line. Error is: Parse error: syntax error, unexpected 'while' (T_WHILE)

Can anyone see what I have done incorrect here?

<?php
    /* prepare statement */
    if ($stmt = $link->prepare("SELECT DISTINCT ticket.TicketuserID, user.UserName,  count(*) AS counter
    FROM ticket
    INNER JOIN user
    ON user.userID= ticket.TicketuserID 
    WHERE `TicketStatus` LIKE 'Active'
    GROUP BY ticket.TicketuserID, user.UserName
    HAVING COUNT(*) >= 1")) {
    $stmt->execute();

    /* bind variables to prepared statement */
    $stmt->bind_result($TicketuserID, $UserName, $counter);


    /* fetch values */
    while ($stmt->fetch()) {
        printf("<a href=\"ticketlist.html\" class=\"list-group-item list-group-item-action\">" . $UserName. " - " . $counter. "</a>");
        //printf("%s %s\n<br>", $UserName, $counter);
    }

        /* close statement */
            $stmt->close();
        }

        /* close connection */
        $link->close();

        ?>
CwStrange
  • 35
  • 7

2 Answers2

0

1st error

You forgot to put semicolon for this code:

/* bind variables to prepared statement */
$stmt->bind_result($UserName, $counter)

2nd error

In your select statement, you want to SELECT 3 things.

ticket.TicketuserID, user.UserName, count(*) AS counter.

but in your bind_result, you only provide 2 variables. You forgot to include variable for the ticket.TicketuserID.

So just do this:

$stmt->bind_result($ticket, $UserName, $counter);
Afif Zafri
  • 640
  • 1
  • 5
  • 11
  • Thanks I fixed that. Now I'm getting the below error, has it got something to do with SQL? Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement – CwStrange Feb 05 '17 at 16:18
  • In your select statement, you want to SELECT 3 things. ```ticket.TicketuserID, user.UserName, count(*) AS counter```. but in your bind_result, you only give 2 variable. You forgot to include variable for the ticket. just do this: ```$stmt->bind_result($ticket, $UserName, $counter);``` – Afif Zafri Feb 05 '17 at 16:21
  • Thank you so much, its now working. I have update the above. Thanks all for help – CwStrange Feb 05 '17 at 16:24
  • You are welcome. Please upvote my answer if you found it useful :) – Afif Zafri Feb 05 '17 at 16:25
0

You are missing semicolon before starting while loop.

$stmt->bind_result($UserName, $counter);

Remember, whenever you see unexpected 'start of code', most probably you did not end the previous line of code.

Archi
  • 485
  • 3
  • 12
  • Thank you for that. I have now added the semicolon. And now I get the below error: Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in – CwStrange Feb 05 '17 at 16:06