Php 警告:为foreach()提供的参数无效,警告:无法修改标题信息-标题已发送

Php 警告:为foreach()提供的参数无效,警告:无法修改标题信息-标题已发送,php,Php,我被以下问题困扰了一段时间 我不断得到以下错误: Warning: Invalid argument supplied for foreach() in /home/content/50/10835750/html/dogdays/game/table.class.php on line 32 Warning: Cannot modify header information - headers already sent by (output started at /home

我被以下问题困扰了一段时间

我不断得到以下错误:

Warning: Invalid argument supplied for foreach() in     
/home/content/50/10835750/html/dogdays/game/table.class.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at    
/home/content/50/10835750/html/dogdays/game/table.class.php:32) in 
/home/content/50/10835750/html/dogdays/game/loadplayer.php on line 77
Redirecting to /game/locations/
我的代码如下:

<?php 
// First we execute our common code to connection to the database and start the session 
define('MyConst', TRUE);

include('database.class.php');
include('table.class.php'); 
include('user.class.php');
include('characters.class.php');
include('pc/player.class.php');
include('pc/human.class.php');
include('loginattempts.class.php');
include('timer.class.php'); 
include('functions.php'); 
include('loginf.php');


$dbo = database::getInstance();
$dbo -> connect("*****************", "***********", "*********", "********", 
array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); 

secSessionStart();

$_SESSION['page'] = "loadplayer.php";

// At the top of the page we check to see whether the user is logged in or not 
if(empty($_SESSION['user'])) 
{ 
    // If they are not, we redirect them to the login page. 
    header("Location: login.php"); 

    // Remember that this die statement is absolutely critical.  Without it, 
    // people can view your members-only content without logging in. 
    die("Redirecting to login.php"); 
} 

// Everything below this point in the file is secured by the login system 

// We can display the user's username to them by reading it from the session array.  
Remember that because 
// a username is user submitted content we must use htmlentities on it before     
displaying it to the user. 
?>
<?php
//    if(empty($_SESSION['playercharacter'])) {
    $character_id = "";
    $queryString = $_SERVER['QUERY_STRING'];
    $queryString = explode("&", $queryString);
    if(strcmp(substr($queryString[0],0,2),"c=") == 0) {
        $character_id = substr($queryString[0],2);
    }

    $c = new characters();
    $c -> load($character_id);
    $playercharacterobject = new human($c, null);
    $_SESSION['playercharacter'] = $playercharacterobject;

    try {
        $stmt = $dbo->getConnection()->prepare("SELECT * FROM location WHERE 
location_id = :location_id");
        $query_params = array(':location_id'=>$playercharacterobject->location_id);       
        // Execute the prepared query.
        $result = $stmt->execute($query_params);
    }
    catch(PDOException $ex) { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query1: " . $ex->getMessage()); 
    }

    $rows = $stmt->fetch(PDO::FETCH_ASSOC);

    $_SESSION['currentlocation'] = $rows['url'];
    $validAccess = array("babelfloor.php");
    $_SESSION['validAccess'] = $validAccess;
    $_SESSION['prevlocation'] = "loadplayer.php";
//    }


//     echo $_SESSION['currentlocation'];
//     echo $rows['url']; 
    // THIS IS LINE 77. THE ONE THAT IS CAUSING PROBLEMS 
    header("Location: /game/locations/".$_SESSION['currentlocation']);
    die("Redirecting to /game/locations/".$_SESSION['currentlocation']);
//      header("Location: combat.php");
//      die("Redirecting to combat.php"); 
?>

为foreach()提供的错误消息
参数无效
表明
foreach
被赋予的东西既不是数组也不是对象

鉴于这段代码:

$dbo = database::getInstance();
$sql = $this->buildQuery('load');
$row = $dbo->query($sql, array(':'.$this->table.'_id'=> $id));

// THIS IS LINE 32
foreach ($row as $key=>$value) {
很明显,
$row
不是您所期望的。可能查询包含无效的sql或数据库中不存在id。您应该进行更多的错误检查以正确处理这些情况


已经发送的消息
标题在这里并不重要。如果您解决了第一个问题,第二个问题也将消失。

table.class.php中的第32行是什么?在使用
header()之前,您不能输出任何html。
也可以使用
is\u array($var)
并查看您得到了什么,我确信您在
foreach
中使用的
$var
不是数组,或者只是将其添加到了代码中。但是,是的,您应该首先解决
foreach
问题。
$dbo = database::getInstance();
$sql = $this->buildQuery('load');
$row = $dbo->query($sql, array(':'.$this->table.'_id'=> $id));

// THIS IS LINE 32
foreach ($row as $key=>$value) {