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']}"; }
online='online', lvl='$content'
, you must at least add the parametersid
andnickname
(unless of course yourid
field is not a unique identifier with auto-increment) + if you want to make Update entries, then you need aWHERE
condition on which we will determine which specific record to update - LamerXaKerиногда уровень
meanиногда уровень
? I understand a comma through the list of fields ...) now I will try) - Konstantinif (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