Please tell me what I'm doing wrong)) here is a code that I don’t understand how to fix correctly:

/* Таблица MySQL, в которой будут храниться данные */ $table = "test_table"; /* Создаем соединение */ mysql_connect($hostname, $username, $password) or die ("Не могу создать соединение"); /* Выбираем базу данных. Если произойдет ошибка - вывести ее */ mysql_select_db($dbName) or die (mysql_error()); /* Составляем запрос для извлечения данных из полей "name", "email", "theme", "message", "data" таблицы "test_table" */ $query = "SELECT id, nickname FROM $table"; /* Выполняем запрос. Если произойдет ошибка - вывести ее. */ $res = mysql_query($query) or die(mysql_error()); function getPage($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'); $data_fin = curl_exec($ch); curl_close($ch); return $data_fin; } $ups = "&noredir=9462fa1c293699b04eb15e8b8d84b9ba"; while ($row = mysql_fetch_array($res)) { $nick_names = $row['nickname']; // пример произвольного массива ник-неймов $length = count($nick_names); $test1 = mysql_query("SELECT name FROM mytable"); for ($i = 0; $i < $length; $i++) { $url = "http://w2.dwar.ru/user_info.php?nick=" . urlencode($nick_names) . $ups; $page = getPage($url); $content = file_get_contents($url); $pos = strpos($content, '&lvl='); $content = substr($content, $pos); $pos = strpos($content, '&tSrc=images'); $content = substr($content, 0, $pos); $content = str_replace('&lvl=','', $content); if (preg_match('/online=1/',$page)) { $query = "INSERT INTO $table SET online='online', lvl='$content'"; } else { $query = "INSERT INTO $table SET online='offline', lvl='$content'"; } } } /* Выполняем запрос. Если произойдет ошибка - вывести ее. */ mysql_query($query) or die(mysql_error()); /* Закрываем соединение */ mysql_close(); 

If instead of INSERT you enter Update, then nothing will happen at all)) (at the moment the script does not work, it creates an additional field with empty data and writes it online or offline ... sometimes another level)

When the script works on the data output to the current page like this:

 if (preg_match('/online=1/',$page)) { echo '<button class="btn-clipboard" data-clipboard-text="prv[' . $nick_names . ']"><img src="http://www.dwar.ru/images/news-arrow.gif"></button> ' . $nick_names . ' [' . $content . ']<a href="' . $url . '" target="_blank"><img src="http://w2.dwar.ru/images/player_info.gif" border=0 width=10 height=10 align="absmiddle"></a> : <span style="color: green;"><b>Online</b></span><br><br>'; } else { echo '<button class="btn-clipboard" data-clipboard-text="prv[' . $nick_names . ']"><img src="http://www.dwar.ru/images/news-arrow.gif"></button> ' . $nick_names . ' [' . $content . ']<a href="' . $url . '" target="_blank"><img src="http://w2.dwar.ru/images/player_info.gif" border=0 width=10 height=10 align="absmiddle"></a> : <span style="color: red;"><b>Offline</b></span><br><br>'; } 

Ie I see a full list of all people from the database, but what level and online or not after processing is issued just to the page, without writing to the database.

How to make it so that after grabbing everything is added to the database? for example by nickname or by id ...

The database contains 4 fields:

 -id -nickname -online -lvl 

Please help, at the moment the processed data is displayed directly on the page, without writing to the database, the fact is that it is necessary to write to the database without output to the page, that it would not be recorded once, but updated, with each request for this code.

Initially, the table having only id and nickname , the entire list should be processed through this code and add (update) all values online and lvl .

What am I doing wrong ? )))

  if (preg_match('/online=1/',$page)) { $query = "UPDATE $table SET nickname = '$nick_names', online = 'online', lvl = '$content' WHERE id = {$row['id']}"; } else { $query = "UPDATE $table SET nickname = '$nick_names', online = 'offline', lvl = '$content' WHERE id = {$row['id']}"; } 
  • Well, you can start with the fact that he introduces exactly what you ask him to make, namely the status and "sometimes level" online='online', lvl='$content' , you must at least add the parameters id and nickname (unless of course your id field is not a unique identifier with auto-increment) + if you want to make Update entries, then you need a WHERE condition on which we will determine which specific record to update - LamerXaKer
  • Everyone’s id is unique, and what does the иногда уровень mean иногда уровень ? I understand a comma through the list of fields ...) now I will try) - Konstantin
  • if (preg_match('/online=1/',$page)) { $query = "UPDATE $table SET lvl='11' WHERE id = '11'"; echo ''. $nick_names . '-онлайн, id-'. $id . ' , уровень - '.$content.'<br />'; Here is the code, the entire list is displayed, everything is fine, but the level does not change to 11, 11, go (what's the trouble? - Constantine
  • Thanks works .. figured out - Konstantin

1 answer 1

Try to get a level like this:

 if(preg_match("~&lvl=(.*)&tSrc~",$html_page,$preg)) print_r($preg); $preg[1] - тут ваш уровень 
  • level I already get $content displays the level)) I can’t torment everything into the database - Konstantin
  • Can you please tell me how to build a query for entering data into a table, I have 20 people, the table has only a column with filled id and nicknames) how to write all this into a table ( - Konstantin
  • Please explain your answer, what it is and what it is eaten with, I already did what I wanted - Konstantin
  • Please explain how it works, to the level that there was less code - Konstantin
  • Such a parser does not work ((please, for the fifth time I ask for help) - Konstantin