CwStrange CwStrange - 4 years ago 128
PHP Question

Failed While - fetch PHP

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();

?>

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download