Find the answer to your Linux question:
Results 1 to 2 of 2
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 ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    3

    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:

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

    Thx

  2. #2
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    mysql_db_query is:

    1] deprecated
    2] used incorrectly like this

    you should refactor your code to use the mysql_query function. further you should implement better exception handling. I can assume that the query you're executing there is either executed on the wrong database or something else goes roughly wrong because you spawn there multiple connections where one connection would be enough. and as last sidenote, one should use mysql parameters and correctly quote variables passed into queries.

    greetings,
    d.

Posting Permissions

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