Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
传递要包含在PHP5.3中的变量时出错_Php_Html_Apache - Fatal编程技术网

传递要包含在PHP5.3中的变量时出错

传递要包含在PHP5.3中的变量时出错,php,html,apache,Php,Html,Apache,Index.php if (!$result) { $error = 'No value found.'; include 'error.html'; exit(); } while ($row = mysqli_fetch_array($result)) { $myvalues[] = $row['ValueID']; } include 'output.html'; exit(); Output.php <?php

Index.php

if (!$result)  
{  
    $error = 'No value found.';
    include 'error.html';  
    exit();  
}  
while ($row = mysqli_fetch_array($result))  
{  
    $myvalues[] = $row['ValueID'];   
}  
include 'output.html';
exit();
Output.php

<?php foreach ($myvalues as $allmyvalues): ?>  
    <?php echo $allmyvalues; ?>  
<?php endforeach; ?> 
错误是未定义的变量:myvalues在…output.php中,页面的其余部分正确显示

使用Apache2.2.15/PHP5.3

你知道怎么了吗?基本上一切都正常运行,但是$myvalue没有被传递。在Windows2K机器上运行这个,所以我不认为升级Apache/PHP是一个选项


提前感谢。

如果从头到尾阅读代码,您会意识到,如果查询中没有返回任何行,那么$myvalues数组将永远无法添加任何内容

PHP可能允许您向未定义的数组添加值,但这本身就是一种糟糕的做法。无论是在循环期间还是在输出结果时,在尝试使用数组之前,都应该将数组声明为空

在使用变量之前,还应该检查变量是否存在,特别是当它们只是有条件地创建时

$myvalues = array();
while($row = mysqli_fetch_array($result))
    $myvalues[] = $row['ValueID'];

// here, you can use foreach on the array, because you've given it an empty default value
条件定义的一个例子是:

if($foo)
    $bar = 'baz';

echo $bar; // could easily be undefined

// You should instead check its existence first

if(isset($bar)) echo $bar;

您也可以在Output.php中编写:


但最好的做法是声明变量$myvalues=array;使用它们之前。

一个原因可能是包含output.html而不是output.php,另一个原因可能是没有将$myvalues声明为数组。请尝试下面的代码

$myvalues=array();
    if (!$result)  
    {  
        $error = 'No value found.';
        include 'error.html';  
        exit();  
    }  
    while ($row = mysqli_fetch_array($result))  
    {  
        $myvalues[] = $row['ValueID'];   
    }  
    include 'output.php';
    exit();

希望这对您有所帮助

问题在于,如果$result为false,您假设没有找到结果;但是,只有在出现查询错误时才会返回该返回值。空结果集仍被视为成功的查询结果。我建议将逻辑稍微移动一点:

$myvalues = array();
if ($result) {
    while (($row = mysqli_fetch_array($result)) !== false) {
        $myvalues[] = $row['ValueID'];
    }
}
if (empty($myvalues)) {
    $error = 'No value found.';
    include 'error.html';  
    exit();  
}  
include 'output.php';

只需声明一个变量$myvalues=array;在您提供的代码之上。您正在引用未初始化的变量致命错误或通知/警告?output.html vs output.php-typo或更糟?@Mahan,这是不必要的。PHP将隐式地将其实例化为数组。关于未定义变量的注意事项;还有一个警告,foreach提供的参数无效,但我假设变量传递问题将解决这个问题。这里Output.html和Output.php是一个打字错误……在代码中两者都是Output.php。
$myvalues = array();
if ($result) {
    while (($row = mysqli_fetch_array($result)) !== false) {
        $myvalues[] = $row['ValueID'];
    }
}
if (empty($myvalues)) {
    $error = 'No value found.';
    include 'error.html';  
    exit();  
}  
include 'output.php';