简体   繁体   中英

How do I count rows in a table according to ID column (prepared statements)?

I have an array populated with records from one table and want to count corresponding records from another table and insert that into the array.

When I try this code I keep getting this error

Warning: mysqli::prepare() [mysqli.prepare]: All data must be fetched before a new statement prepare takes place

foreach ($persons as $i=>$person)
{ 
    $stmt = $mysqli->prepare("SELECT COUNT(*) FROM order WHERE personId = ?");
    $stmt->bind_param("i", $person['personId']);
    $stmt->execute();
    $stmt->bind_result($totalOrders);
    $stmt->fetch();
    $stmt->close;       
    $persons[$i]['totalOrders'] = $totalOrders;
}

It's as though the $stmt->close; is being ignored.

您需要添加括号以调用close方法:

$stmt->close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM