0

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

3
  • Where is the line 164? Your file, here, has only 100 lines... How can we guess the line problem then? Commented Sep 3, 2012 at 21:56
  • Good grief! What's wrong with foreach? Commented Sep 3, 2012 at 22:01
  • Can you print_r($name) after the $name variable is set in the while loop to see what value it gets from the key function?? Post the results Please prnt_r $data before the loop also Commented Sep 3, 2012 at 22:05

1 Answer 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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Cuonic Seriously, take a look at foreach. It will change your life!
I do know about foreach, just didn't think of using it, will change my script if it makes you feel any better :]
@DaveRandom I have no idea though on how to make this work with foreach, I've tried one or two things but keep getting illegal offset errors ? Could you provide a code example based on what I provided with the while loop ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.