Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    2

    Unanswered: ping hosts from database

    I have a php script
    I want it to ping list of ips from a databse(using fping) and based on the status of the values to put them in a field(status) into the database(0 if down,1 if up)
    i executed tghe script and it gives me this error:
    Thread 1 - 17:51:01 26-04-2011 4.2.2.2 alive Couldn't execute query.
    I cant see what 's wrong

    this is the source of the php script:
    Code:
    <?php
    require_once("conf.php");
    $connection1 = mysql_connect($h, $u, $p) or die("Couldn't connect.");
    
    
    $db_name = "status";
    $table_name = "voip";
    $connection = mysql_connect($h, $u, $p) or die("Couldn't connect.");
    $db = mysql_select_db($db_name, $connection) or die("Couldn't select database.");
    $sql = "SELECT ip
            FROM $table_name where thread = '1' ORDER BY ip
                    ";
        $result = mysql_query($sql,$connection) or die("Couldn't execute query1...");
        while ($row = mysql_fetch_array($result)) {
                                    $ip = $row['ip'];
    
    
    $comanda = "/usr/bin/fping ". $ip;
    $str=exec($comanda);
    
    list($ip, $is, $status) = sscanf($str, "%s\t%s\t%s\n");
    
    $dataa= date("H:i:s d-m-Y");
    
    print "Thread 1 - ";
    print $dataa;
    print "\t";
    print $ip;
    print "\t\t";
    print $status;
    print "\n";
    
    
    
    $dbname="status";
    $db1 = mysql_select_db($dbname, $connection1) or die("Couldn't select database.");
    $sql1 = "UPDATE voip SET status = '$status', lastupdate = '$dataa' WHERE ip = '$ip'";
    $result1 = mysql_db_query($dbname,$sql1) or die("Couldn't execute query.");
    
    }
    ?>
    this is the database:
    database: status
    table: voip
    fields: id ip status thread
    1 4.2.2.2 1
    2 4.2.2.2 1
    3 4.2.2.2 1

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    • Why don't you just print out the $sql value in the die command? that way you can see what's actually being run.
    • Why are the table and database names in variables?
    • Why do you have search odd variable names $comanda $dataa? $sqll
    • Your code is very difficult to read and the indenting is very strange.
    • Is that date format correct for MySQL? I thought it should be YYYY-MM-DD HH:mm.
    • Bits like the following :
      Code:
      print "Thread 1 - ";
      print $dataa;
      print "\t";
      print $ip;
      print "\t\t";
      print $status;
      print "\n";
      can also be shortened into :
      Code:
      print "$dataa\t$ip\t\t$status\n";

  3. #3
    Join Date
    Apr 2011
    Posts
    2

    thx

    Yes..it was the date problem
    it works fine now
    Thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •