简体   繁体   English

使用PDO从MySQL获取并循环遍历数据

[英]Fetch and loop through the data from MySQL using PDO

I want to loop through all rows in the database and display them to the user, but I'm only getting the first row in my output. 我想遍历数据库中的所有行并将它们显示给用户,但是我只会在输出中得到第一行。

Here is my code:: 这是我的代码:

Query function: 查询功能:

public function query($sql, $params = array()) {
    $this->_error = false;
    if($this->_query = $this->_pdo ->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach ($params as $param) {
                $this->_query->bindValue($x,$param);
                $x++;
            }
        }

        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count =$this->_query->rowCount();
            } else {
                $this->_error = true;
            }

        }
        return $this;
    }


public function results() {
    return $this->_results;
}

My query: 我的查询:

$search = DB::getInstance()->query("SELECT * FROM staff_info WHERE staff_id = ?",array(
Input::get('staff_id')));

if($search->count()){
if($search) {
    $search_result = $search->results();
    foreach ($search_result as $val) {
        $fname = $val->fname;
        $surname = $val->surname;
        $street = $val->street;

        echo '<div class="table-responsive table-bordered">
        <table class="table">
        <tr>
        <td><a href="index.php?ad=profile">'.$fname.'</a></td>
        <td><a href="index.php?ad=profile">'.$surname.'</td>
        <td><a href="index.php?ad=profile">'.$street.'</td>
        </tr>
        </table>
        </div>';
    }
}

The problem is -- I am getting only the first row even if there are more other rows in the database. 问题是-即使数据库中还有其他行,我也只能得到第一行。 Any ideas why this is happening? 任何想法为什么会这样?

You have missed out the underscore. 您错过了下划线。

In your query function you are storing output in $this->_results . 在查询函数中,您将输出存储在$this->_results You need to correct your variable name before looping. 您需要在循环之前更正变量名。 $search->_results . $search->_results

Using PDO methods prepare() ,execute() & fetchAll() or fetch(). 使用PDO方法prepare() ,execute()和fetchAll()或fetch()。 Notice I have called my method findAll() so as not to confuse it with PDOs fetchAll() . 注意,我已经调用了我的方法findAll()以免将其与PDO fetchAll()混淆。 The following code is based on my usage. 以下代码基于我的用法。

In Class 在班上

For all results 对于所有结果

public function findAll($sql,$param) {
    $query = $this->dbh->prepare($sql);
    if(is_array($param)){
        $query->execute($param);//1 parameter
    }else { 
        $query->execute(array($param));// >1 parameters
    }
    $rows = $query->fetchAll();
        if($rows){
          return $rows;
        }
    }
  }

For one result 为了一个结果

public function findOne($sql,$param) {
        $query = $this->dbh->prepare($sql);
        if(is_array($param)){
            $query->execute($param);//1 parameter
        }else { 
            $query->execute(array($param));// >1 parameters
        }
        $row = $query->fetch();
            if($row){
              return $row;
            }
        }
      }

Usage 用法

$params=array(100,"AK");
 $sql1 = "SELECT * FROM users WHERE id < ? AND state = ?";
 $rows = $dao->findAll($sql,$params);
 foreach($rows as $row){//Remove for 1 result
    echo $row['firstname'];
    etc.....

if you want to find one record only you can replace fetchAll() with fetch() in a new method and just echo without foreach() 如果您只想查找一条记录,则可以在新方法中将fetchAll()替换为fetch() ,并且仅回显而不使用foreach()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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