Liam J
Liam J

Reputation: 163

PHP throwing "Undefined Index:" for existing array

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.

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

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

<?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 :)

Upvotes: 0

Views: 334

Answers (1)

Joe C.
Joe C.

Reputation: 1538

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.

Upvotes: 2

Related Questions