当数据库查询返回no / empty结果时,Foreach循环中断

时间:2015-05-03 21:15:52

标签: php mysql arrays mysqli foreach

我有一个foreach循环来显示来自query的数据,当查询没有返回任何结果时 - 有时候我希望它不显示任何数据,但我得到了以下错误:

  

警告:第29行/home/u913241783/public_html/editmeeting.php中为foreach()提供的参数无效

显示数据的细分是:

<?php
    foreach($activities as $k=>$v) {
?>
<tr class="table-row">
    <td><?php echo $k+1; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'title', 'activities', 'activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["title"]; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'description','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["description"]; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'leaders','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["leaders"]; ?></td>
    <td contenteditable="true" onBlur="saveToDatabase(this,'time','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["time"]; ?></td>
</tr>
<?php
    }

修改

完整代码是:

<?require_once("dbcontroller.php");
$db_handle = new DBController(); ?>
<h1 class="regular brown bottom_line">Activity Test</h1>
<div class="clear"></div>
</header>

<?
include'connect_db.php';
//get variables
$input1 = $_SESSION[ 'unitid' ];
$id=($_GET['id']);
$activities=array();

        $sql = "SELECT activityid, activitynumber, title, description, time, leaders FROM activities WHERE meetingid='$id'";
        $activities = $db_handle->runQuery($sql);?>
       <table class="tbl-qa">
          <thead>
              <tr>
                <th class="table-header" width="10%">Activity No.</th>
                <th class="table-header">Title</th>
                <th class="table-header">Description</th>
                <th class="table-header">Leaders</th>
                <th class="table-header">Time</th>

              </tr>
          </thead>
          <tbody>
          <?php
          print_r($activities);
          foreach($activities as $k=>$v) {
          ?>
              <tr class="table-row">
                <td><?php echo $k+1; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'title', 'activities', 'activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["title"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'description','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["description"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'leaders','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["leaders"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'time','activities','activityid','<?php echo $activities[$k]["activityid"]; ?>')" onClick="showEdit(this);"><?php echo $activities[$k]["time"]; ?></td>
              </tr>
        <?php
        }
        ?>
          </tbody>
        </table>
    </body>
</html>

dbcontroller.php

<?php
class DBController {
    private $host = "***********";
    private $user = "***********";
    private $password = "***************";
    private $database = "***********";



    function __construct() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database) OR die ( mysqli_connect_error() );
    }

    function runQuery($query) {
        $conn = mysqli_connect($this->host,$this->user,$this->password);
        mysqli_select_db($conn, $this->database);
        $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysqli_query($conn, $query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;   
    }
}
?>

1 个答案:

答案 0 :(得分:0)

问题在于您的方法runQuery,因为如果结果为空,它不会返回任何内容。只需添加一个else case,只返回一个空数组,这样foreach循环就不会运行,例如。

function runQuery($query) {
    $conn = mysqli_connect($this->host,$this->user,$this->password);
    mysqli_select_db($conn, $this->database);
    $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
        return $resultset;
    else
        return array();
}

我还建议您将连接保存到属性中,然后将该属性用作连接变量,例如

function __construct() {
    $this->conn = mysqli_connect($this->host,$this->user,$this->password,$this->database) OR die ( mysqli_connect_error() );
  //^^^^^^^^^^^
}   

然后您可以在$this->conn的其他方法中使用它。

你也可能想看看mysqli_* prepared statements它更安全