Answer the question
In order to leave comments, you need to log in
How to update multiple records in MySQL using PHP?
Good evening. I continue to turn my admin panel, the following plug has arisen - there is a script that receives data from the main page with a GET and pings and parses the page header at the received address, thereby showing whether the address is available or not. I want to transfer this functionality to a separate script and run it via cron so that it automatically pings all sites and writes their status to the database. Here, in fact, is the plug - how can one script take the entire array of IPs, check each one, and update the record for each IP in the database? I select the fields Id (unique identifier), IP (the address itself) and State (has the values 0, 1, 2, or is still empty (the site has never been checked)).
Below is the code for the first part that I could compile from everything I know.
<?php
// Коннектимся к БД
include 'connect.php';
// подключаемся к базе данных
mysql_select_db($db_name, $connect_to_db)
or die("Could not select DB: " . mysql_error());
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET SESSION collation_connection = 'utf8_general_ci'");
$qr_result = mysql_query("select `Id`, `IP`, `State` from `table4`")
or die(mysql_error());
//объявляем переменные
$data = mysql_fetch_array($qr_result);
$ip=$data['IP'];
if (!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',$ip)) {echo "Sorry, wrong request"; exit;}//проверка на правильность записи
preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',"$ip", $matches);//удаление порта, если есть
$ip2=$matches[0];
$ch = curl_init( $ip );
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_NOBODY => 1
);
curl_setopt_array( $ch, $options );
curl_exec($ch);
$http = curl_getinfo($ch);
curl_close($ch);
exec("ping -c 1 -w 10 $ip2",$output, $status);
<?php
// Коннектимся к БД
include 'connect.php';
// подключаемся к базе данных
mysql_select_db($db_name, $connect_to_db)
or die("Could not select DB: " . mysql_error());
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET SESSION collation_connection = 'utf8_general_ci'");
$qr_result = mysql_query("select `Id`, `IP`, `State` from `table4`")
or die(mysql_error());
//объявляем переменные
while ($data = mysql_fetch_array($qr_result)){
$ip=$data['IP'];
$id=$data['Id'];
$state=$data['State']}
if (!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',$ip)) {echo "Sorry, wrong request"; exit;}//проверка на правильность записи
preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',"$ip", $matches);//удаление порта, если есть
$ip2=$matches[0];
$ch = curl_init( $ip );
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_NOBODY => 1
);
curl_setopt_array( $ch, $options );
curl_exec($ch);
$http = curl_getinfo($ch);
curl_close($ch);
exec("ping -c 1 -w 10 $ip2",$output, $status);
if ($http['http_code']=="200" and $status==0)
{$state="0";}
elseif ($http['http_code']=="200" and $status!=0)
{$state="0";}
elseif ($http['http_code']!="200" and $status==0)
{$state="1";}
else
{$state="2";}
mysql_query("UPDATE `table4` SET `State` =$state WHERE `Id` =$id");
};
?>
Answer the question
In order to leave comments, you need to log in
php.net/manual/ru/function.mysql-query.php
in your case it will look something like this:
...
$qr_result = mysql_query("select `Id`, `IP`, `State` from `table4`")
or die(mysql_error());
while ($data = mysql_fetch_array($qr_result)) {
//объявляем переменные
$ip=$data['IP'];
if (!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',$ip)) {echo "Sorry, wrong request"; exit;}//проверка на правильность записи
preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',"$ip", $matches);//удаление порта, если есть
$ip2=$matches[0];
...
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question