【问题标题】:PHP Statement Should return Multiple objects but only returns onePHP 语句应该返回多个对象但只返回一个
【发布时间】:2012-02-28 22:16:38
【问题描述】:

对于我的一生,我无法弄清楚为什么我的 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 变量的客户。但是,它只返回第一个客户,仅此而已。

有什么想法吗?

另外,我想做的一件事是将名字和姓氏连接起来,但我不确定我是否可以在 PHP 中做到这一点。我还在学习这个。

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    应该是:

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

    【讨论】:

      【解决方案2】:

      您在第一次调用 mysqli_stmt_fetch() 后返回,所以当然只有一个对象。 创建一个数组,在 while 循环中运行 mysqli_stmt_fetch() 并将所有行添加到数组中,然后返回数组。

      查看示例比在此处发布要快 http://ca3.php.net/manual/en/mysqli-stmt.bind-result.php

      【讨论】:

      • 感谢您提供的链接,我已经阅读了相当多的内容,它开始变得有意义了。
      猜你喜欢
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多