G
G
Gorthaur2015-10-11 21:51:58
PHP
Gorthaur, 2015-10-11 21:51:58

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

IT'S ALIVE!!!
I did not expect that it would work the first time (excluding a couple of jambs in the form of a forgotten parenthesis or a question mark in the last line of code), but it worked!
<?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

1 answer(s)
R
Ruslan Fedoseev, 2015-10-11
@gorthaur

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];

...
};

Something like this.
Now they will come running and say - do not use mysql_query. Well, actually it is written at the beginning of the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question