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

时间:2022-10-17 10:55:56

I'm developing a script for checking the state of different services on a hosting platform, which sends the state to a database (0 : Online / 1 : Disfunctional / 2 : Offline / 3 : Unknown) for each service.

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

I'm receiving the following error : Notice: Undefined index: in /home/cuonicco/public_html/olympe/inc/updatestatus.php on line 164

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

Here is the code, I have cut out some parts (the other 3 different verifications) :

这是代码,我已经删除了一些部分(其他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++;
}

?>

What exactly is causing this, it's been bugging me all day. I have echoed out the values of the arrays before the "error line" and they all exist.

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

Thanks in advance :)

提前致谢 :)

1 个解决方案

#1


2  

Without knowing which line is throwing the error, I'm still 95% certain your problem is with while($i <= $array_count). When iterating over arrays, the rule of thumb to prevent yourself from going out of bounds is that you use strictly less-than < when indexing starts at 0, and use less-than-or-equal-to <= when indexing starts at 1. Try switching <= with < and see if that removes your error.

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

#1


2  

Without knowing which line is throwing the error, I'm still 95% certain your problem is with while($i <= $array_count). When iterating over arrays, the rule of thumb to prevent yourself from going out of bounds is that you use strictly less-than < when indexing starts at 0, and use less-than-or-equal-to <= when indexing starts at 1. Try switching <= with < and see if that removes your error.

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