PHP语句应该返回多个对象,但只返回一个


PHP Statement Should return Multiple objects but only returns one

就我的生活而言,我不明白为什么PHP脚本中的以下语句只返回一个对象。它是一个与之通信的mysql服务器,到目前为止我已经写了这篇文章:

公共函数getCustomerinfoByCompanyID($itemID){

    $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where CompanyID=?");
    $this->throwExceptionOnError();
    mysqli_stmt_bind_param($stmt, 'i', $itemID);        
    $this->throwExceptionOnError();
    mysqli_stmt_execute($stmt);
    $this->throwExceptionOnError();
    mysqli_stmt_bind_result($stmt, $row->CustID, $row->CompanyID, $row->FirstName, $row->Surname, $row->CellNo, $row->Email);
    if(mysqli_stmt_fetch($stmt)) {
      return $row;
    } else {
      return null;
    }

现在,据我所知,它应该返回所有CompanyID为variable的客户。然而,它只是返回第一个客户,仅此而已,没有其他内容。

有什么想法吗?

此外,我想做的一件事是连接FirstName和Surname,但我不确定我是否能在PHP中做到这一点。我还在学习这个。

应该是:

while( mysqli_stmt_fetch($stmt)) {
   $rows[] = $row;
}
您在第一次调用mysqli_stmt_fetch()之后返回,所以当然只有一个对象。创建一个数组,在while循环中运行mysqli_stmt_fetch(),并将所有行添加到该数组中,然后返回该数组。

浏览示例比在这里张贴要快http://ca3.php.net/manual/en/mysqli-stmt.bind-result.php