【问题标题】:php mysqli stmtphp mysqli stmt
【发布时间】:2010-09-27 09:37:38
【问题描述】:

我是 php mysql 用法的新手。我正在从数据库中选择结果..我正在使用 while($stmt->fetch()): .. 我想要做的是再次循环浏览结果在第一个循环之后不调用数据库(frombuffered results set)。

我在 xampp 服务器上使用 php5、mysqli、stms。

【问题讨论】:

    标签: php mysqli


    【解决方案1】:
    while($row = $stmt->fetch()){
      $storedRows[] = $row;
      //do stuff
    }
    foreach($storedRows as $row){
      //do more stuff
    }
    

    【讨论】:

    • 该死的,这个 stackoverflow 有效!!该死! :-) 感谢您的回答
    【解决方案2】:

    你可以使用数组。

    当您第一次循环遍历结果时,您可以将值放入数组中,稍后在第二次循环中,您可以访问数组中的元素。

    类似:

    $query = "SELECT name FROM EMP";
    
    $arr = array();
    if ($stmt = $mysqli->prepare($query)) {
    
            $stmt->execute();                                  
    
            $stmt->bind_result($name);
    
            // 1st cycle.
            while ($stmt->fetch()) {
                    $arr[] = $name; // save in array.
            }   
    
            $stmt->close();
    
            // 2nd cycle.
            foreach($arr as $name) {
                    // use $name again.
            }   
    }
    

    【讨论】:

    • 谢谢你!我会将这些行直接存储到我的代码库中;-)
    • 在您的这个示例中,使用多个字段的最佳方法是什么?假设我们有 $street、$name 和 $phone
    • 可以使用多维数组。 $arr[] = array($name,$street,$phone);
    • 我明白了...我必须将数组设置为 $arr[] 而不仅仅是 $name
    猜你喜欢
    • 1970-01-01
    • 2014-03-06
    • 2016-12-05
    • 2014-06-26
    • 2017-08-28
    • 2023-03-24
    • 2018-01-12
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多