PHP语句应返回多个对象,但仅返回一个 [英] PHP Statement Should return Multiple objects but only returns one

查看:121
本文介绍了PHP语句应返回多个对象,但仅返回一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我生命中,我无法弄清楚为什么我的PHP脚本中的以下语句仅返回一个对象.这是一个正在与之通信的mysql服务器,到目前为止,我已经写了这篇文章:

For the life of me, I cannot figure out why the below statement in my PHP script returns only one object. It is a mysql server it is communicating with and I have written this so far:

公共函数getCustomerinfoByCompanyID($ itemID){

public function 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为变量的客户.但是,它只返回第一个客户,仅此而已.

Now as far as I know, it should return all of my customers that have an CompanyID of variable. However, it just returns the first customer and that is all, nothing else.

有什么想法吗?

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

Also, one thing I want to do is concatenate FirstName and Surname, but I am not sure if I can do that in PHP. I am still learning this.

推荐答案

应为:

while( mysqli_stmt_fetch($stmt)) {
   $rows[] = $row;
}

这篇关于PHP语句应返回多个对象,但仅返回一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆