Php 如何从类调用while循环?

Php 如何从类调用while循环?,php,pdo,Php,Pdo,我有类getinfo.php和脚本welcome.php。在get info类中,我有一个函数GetAll。我想通过变量$this->first调用all first,并且只返回一个值,但我希望行中的all first。请参见下文 //script getinfo.php class GetInfo{ function getAll(){ global $conn; $sql = $conn->prepare("SELEC

我有类getinfo.php和脚本welcome.php。在get info类中,我有一个函数GetAll。我想通过变量$this->first调用all first,并且只返回一个值,但我希望行中的all first。请参见下文

    //script getinfo.php
class GetInfo{
     function getAll(){
             global $conn;
            $sql = $conn->prepare("SELECT * FROM users");
            $sql->execute();
            while($row = $sql->fetch(PDO::FETCH_ASSOC)){
            $this->first = $row['first'];
            $this->last = $row['last'];
            $this->email = $row['email'];
            }

        }
}
        //script welcome.php

     <?php
      $info = new GetInfo();
      $info->getAll();
      echo $info->first;
      //this is returning only one value, but I want to get all row values.
      ?>
让它成为数组的列表

    class GetInfo{
        function getAll(){
            global $conn;
            $sql = $conn->prepare("SELECT * FROM users");
            $sql->execute();
            while($row = $sql->fetch(PDO::FETCH_ASSOC)){
                $this->first[] = $row['first'];
                $this->last[] = $row['last'];
                $this->email[] = $row['email'];
            }
            return $this;
        }
    }
您的代码:

实际上只是将$this->first,$this->last,$this email设置为从数据库获取的每一行的新值。因此,使用$this->first时只返回一个值

我不确定我是否理解了你的意图,但如果我理解了

您可以使用一个带有数组的函数作为参数,告诉您要使用哪些列,如下所示:

//script getinfo.php
protected function getAll( $use_columns = array() ){
    global $conn;
    $sql = $conn->prepare("SELECT * FROM users");
    $sql->execute();

    $sql_arr = array();
    $row_index = 0;
        while($row = $sql->fetch(PDO::FETCH_ASSOC)){
            foreach($use_columns as $uc) {
                $sql_arr[$row_index][$uc] = $row[$uc];
            }
            $row_index++;
        }    
    return $sql_arr;
}

 //script welcome.php

 <?php
  $info = new GetInfo();
  echo $info->getAll( array('first') ); //Print out all rows with first-value
  echo $info->getAll( array('last') ); //Print out all row with last-value
  echo $info->getAll( array('first', 'last') ); //Print out all row with first and last value
  ?>
注意!我不确定上面的作品是否100%未经测试,但我希望你能理解这个大意

注2!确保验证值,这样用户就不能输入任何值 发送到getAll函数的数组参数中


为什么要设置$this->first等等。?为什么不让getAll返回一个行数组呢?我想在不同的脚本中使用这个函数,并调用特定的信息。在某个地方,我只希望最后一个地方,只有电子邮件或所有表值。那么,获取一个所有行的数组,但只使用所需的值有什么错呢?
//script getinfo.php
protected function getAll( $use_columns = array() ){
    global $conn;
    $sql = $conn->prepare("SELECT * FROM users");
    $sql->execute();

    $sql_arr = array();
    $row_index = 0;
        while($row = $sql->fetch(PDO::FETCH_ASSOC)){
            foreach($use_columns as $uc) {
                $sql_arr[$row_index][$uc] = $row[$uc];
            }
            $row_index++;
        }    
    return $sql_arr;
}

 //script welcome.php

 <?php
  $info = new GetInfo();
  echo $info->getAll( array('first') ); //Print out all rows with first-value
  echo $info->getAll( array('last') ); //Print out all row with last-value
  echo $info->getAll( array('first', 'last') ); //Print out all row with first and last value
  ?>