0

I'm trying to write code to get ip from a table and ping and insert ip and response time another table. What I wrote is given below. It echo ip and response time but only ips are inserted to the table. Response times are blank.

Schema

+-------+-------------+------+-----+---------+--------------‌​--+ 
| Field | Type        | Null | Key | Default | Extra          | 
+-------+-------------+------+-----+---------+--------------‌​--+ 
| id    | int(6)      | NO   | PRI | NULL    | auto_increment | 
| ip    | varchar(30) | NO   |     | NULL    |                | 

+-----------+-------------+------+-----+-------------------+‌​-------+ 
| Field     | Type        | Null | Key | Default           | Extra | 
+-----------+-------------+------+-----+-------------------+‌​-------+ 
| Log_time  | timestamp   | NO   |     | CURRENT_TIMESTAMP |       | 
| ip        | varchar(30) | NO   |     | NULL              |       | 
| Ping_time | varchar(30) | YES  |     | NULL              |       | 

Code

<?php
 // Connects to your Database

mysql_connect("localhost","xxx","xxx");
//database connection
mysql_select_db("xxxxx") or die(mysql_error()); 

$sql="select * from iplist where id=(SELECT MAX(id) FROM iplist)";
$data = mysql_query($sql)  or die(mysql_error()); 
//asing ip to variables 

$row = mysql_fetch_assoc($data);
$id=$row["id"];
$id1=$id;
while($id1>0) {
    $sql="select ip from iplist where id=($id1)";
    $data = mysql_query($sql)  or die(mysql_error()); 
    $row = mysql_fetch_assoc($data);
    $ip=$row["ip"];

    //ping ips
    $ip_ad=$ip;
    $ping = system("ping -c 1 $ip_ad  | head -n 2 | tail -n 1 | awk '{print $7}'", $ping_time);
    print $ping_time[0]; // First item in array, since exec returns an array.
    $responcetime = $ping_time[0];
    $dilay=$responcetime;
    echo $ip;
    echo $dilay;
    //send data to database
    //inserting data order
    $order = "INSERT INTO Ping_log (ip, Ping_time) VALUES ('$ip','$dilay')";
    //declare in the order variable
    $result = mysql_query($order);  //order executes
    $id1 --;
}
?>
mymiracl
  • 583
  • 1
  • 16
  • 24
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 25 '17 at 10:50
  • 1
    Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 25 '17 at 10:50
  • @RiggsFolly do you strangle them yourself? – e4c5 Jan 25 '17 at 10:52
  • if response times are blank why do you expect the database to have non blank value – e4c5 Jan 25 '17 at 10:53
  • @e4c5 Certainly NOT. I am hoping to seek and destroy all the cat stranglers. – RiggsFolly Jan 25 '17 at 10:54
  • This is the wierdest (and may I suggets the most unreliable) way or processing a set of data I have ever seen – RiggsFolly Jan 25 '17 at 10:55
  • Why do you use 2 variables for each value? – RiggsFolly Jan 25 '17 at 11:02
  • Please show us the schema for these 2 tables. – RiggsFolly Jan 25 '17 at 11:04
  • @RiggsFolly I have realized that the use of `mysql_` is often part of the basic lesson when it comes to learn php. When teachers are so old-school, what should a pupil do? And on the other side when a newbie googles: MYSQL + PHP most hits are in relevance to mysql extention ;-) But just a sidenote. – JustOnUnderMillions Jan 25 '17 at 11:05
  • You are doing ABSOLUTELY no error checking on any of your `mysqli_query()` commands. Start by doing that – RiggsFolly Jan 25 '17 at 11:05
  • @JustOnUnderMillions Yea, true. We should start a group that post comments on all these sites suggestion that such _tutorials_ are removed or replaced. _Do you think that would work_? – RiggsFolly Jan 25 '17 at 11:06
  • 1
    @RiggsFolly possible, but it will become a neverending story i think. How about to ask SO to detect use of `mysql_` in Q to autogenerate an information about `mysql_` so we dont have to post it everytime into a comment. – JustOnUnderMillions Jan 25 '17 at 11:09
  • @JustOnUnderMillions Awwwww, do you want to spoil the only fun I get here :) :) – RiggsFolly Jan 25 '17 at 11:11
  • +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(6) | NO | PRI | NULL | auto_increment | | ip | varchar(30) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ – user3916665 Jan 25 '17 at 11:12
  • +-----------+-------------+------+-----+-------------------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+-------------------+-------+ | Log_time | timestamp | NO | | CURRENT_TIMESTAMP | | | ip | varchar(30) | NO | | NULL | | | Ping_time | varchar(30) | YES | | NULL | | +-----------+-------------+------+-----+-------------------+-------+ – user3916665 Jan 25 '17 at 11:13
  • @RiggsFolly SQLInjections and many other stick&stones still noteable here. ;-) – JustOnUnderMillions Jan 25 '17 at 11:14
  • 1
    What the heck is this script? It makes absolutely no sense. – Charlotte Dunois Jan 25 '17 at 11:15
  • You are so on the money there @CharlotteDunois http://stackoverflow.com/questions/41849575/ping-ips-and-save-resuts-in-mysql-db?noredirect=1#comment70883426_41849575 – RiggsFolly Jan 25 '17 at 11:19
  • I note that you were told about the deprecation of the `mysql_` library back in [2014](http://stackoverflow.com/a/25987565/2310830) but still you persist in using it. – RiggsFolly Jan 25 '17 at 11:32

0 Answers0