PHP为现有数组抛出“Undefined Index:”

时间:2012-09-03 21:54:13

标签: php arrays indexing

我正在开发一个脚本来检查托管平台上不同服务的状态,该脚本将状态发送到每个服务的数据库(0:Online / 1:Disfunctional / 2:Offline / 3:Unknown)。 / p>

我收到以下错误:注意:未定义的索引:在第164行的/home/cuonicco/public_html/olympe/inc/updatestatus.php

这是代码,我已经删除了一些部分(其他3种不同的验证):

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$info = array();
$data = array();

$mysqli = new mysqli("*********", "*********", "*********", "*********");

$query = $mysqli->prepare("SELECT name, status FROM olympe");
$query->bind_result($name, $status);
$query->execute();
$query->store_result();
$count = $query->num_rows;

if($count > 0)
{
    while($query->fetch())
    {
        $info[$name]['status'] = $status;
    }
}

$query->close();

// Verification Panel :

$panel = curl_init("https://hosting.olympe.in/login");
curl_setopt($panel, CURLOPT_TIMEOUT, 10);
curl_setopt($panel, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($panel, CURLOPT_HEADER, FALSE);
curl_setopt($panel, CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($panel);
$http_status = curl_getinfo($panel, CURLINFO_HTTP_CODE);
curl_close($panel);

if($http_status == 200)
{
    $data['panel']['status'] = 0;
}
elseif($http_status == 500)
{
    $data['panel']['status'] = 1;
}
else
{
    $data['panel']['status'] = 2;
}

// Verification HTTP :

$http = curl_init("http://cuonic.olympe.in/status/test-http.php");
curl_setopt($http, CURLOPT_TIMEOUT, 10);
curl_setopt($http, CURLOPT_HEADER, FALSE);
curl_setopt($http, CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($http);
$http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
curl_close($http);

if($http_status == 200)
{
    $data['http']['status'] = 0;
}
else
{
    $data['http']['status'] = 2;
}

// More verification functions...
// ....
// Database updating :

$array_count = count($data);
$i = 0;

while($i <= $array_count)
{
    $name = key($data);
    $array = current($data);

    if($info[$name]['status'] != $data[$name]['status']) // Error occurs here
    {
        $query = $mysqli->prepare("INSERT INTO log (name, previous, latest) VALUES (?, ?, ?)");
        $query->bind_param("sii", $name, $info[$name]['status'], $data[$name]['status']);
        $query->execute();
        $query->close();
    }

    $query = $mysqli->prepare("UPDATE olympe SET status = ? WHERE name = ?");
    $query->bind_param("is", $data[$name]['status'], $name);
    $query->execute();
    $query->close();

    next($data);
    $i++;
}

?>

究竟是什么造成了这种情况,这一直困扰着我。我已经在“错误行”之前回显了数组的值,它们都存在。

提前致谢:)

1 个答案:

答案 0 :(得分:2)

在不知道哪一行引发错误的情况下,我仍然95%确定您的问题是while($i <= $array_count)。当迭代数组时,防止自己超出范围的经验法则是,当索引从0开始时使用严格小于<,并使用小于或等于{{1当索引从1开始时,请尝试使用<=切换<=并查看是否会删除您的错误。