WIK Posted November 18, 2020 Report Share Posted November 18, 2020 Am stocat in baza de date intr-o singura coloana sub urmatorul format : 127.0.0.1 13:29:05 13/08/2020 // IP / ORA / DATA Acum as vrea sa fac un cod in PHP care sa-mi extraga doar IP-ul si sa il compare cu alte IP-uri din aceiasi coloana si sa-mi afiseze de cate ori se repeta (doar IP-ul) Se poate ceva de genul sau este greu sa fac asta ? Quote Link to comment Share on other sites More sharing options...
Wav3 Posted November 18, 2020 Report Share Posted November 18, 2020 (edited) Cauta pe net "php mysql select" si ruleaza query-ul: SELECT ip, COUNT(*) AS cnt FROM table_name GROUP BY ip si vei avea lista unica de ipuri si numarul de repetitii pentru fiecare. EDIT: Acum am vazut ca ai zis ca ai toate informatiile in aceasi coloana. in loc de IP poti pune SUBSTRING_INDEX(nume_coloana, " ", 1) daca esti sigur ca ai intotdeauna IP si ca este pe pozitia 1 (pana in primul spatiu) si ca nu contine spatii (adica sa fie fix ca in exemplul tau). Edited November 18, 2020 by Wav3 1 Quote Link to comment Share on other sites More sharing options...
Nytro Posted November 18, 2020 Report Share Posted November 18, 2020 Vrei sa vezi de cate ori se repeta toate IP-urile pe care le ai in DB? In primul rand, daca faci operatii pe acele IP-uri ar trebui sa le "scoti" din acel VARCHAR/TEXT si sa le pui separat intr-o coloana, ti-ar simplifica munca. Dar nu e necesar. SELECT SUBSTRING(ip, 1, LOCATE(" ", ip)) FROM tabel; Quote Link to comment Share on other sites More sharing options...
Moderators Dragos Posted November 18, 2020 Moderators Report Share Posted November 18, 2020 (edited) Daca vrei sa mergi in continuare pe structura pe care o ai in prezent, poti folosi regex sa-ti extragi datele si dupa sa lucrezi cu ele, spre exemplu <?php $mysql = mysqli_connect("localhost","ips","8hLXKFoFBuLld9LW","tests") or die("connection failure"); $q = $mysql->query("select val from ips"); $ips = array(); while($v = $q->fetch_row()) { preg_match('/((?:[0-9]{1,3}\.){3}[0-9]{1,3}) ([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]) ([0-2]?[0-9]\/[0-5]?[0-9]\/[0-9][0-9][0-9][0-9])/', $v[0], $matches); // scoatem prin regex IP-ul, ora si data din randul de mysql // echo $matches[1]; // IP // echo $matches[2]; // Ora // echo $matches[3]; // Data $ips[] = $matches[1]; // pusham IP-ul din randul de mysql in array-ul de IPs } print_r(array_count_values($ips)); // afisam de cate ori apare fiecare IP Cu valorile urmatoare in baza de date 127.0.0.1 13:29:05 13/08/2020 123.123.123.123 13:29:05 13/08/2020 127.0.0.1 13:29:05 13/08/2020 scriptul returneaza Array ( [127.0.0.1] => 2 [123.123.123.123] => 1 ) Edited November 18, 2020 by Dragos 2 Quote Link to comment Share on other sites More sharing options...
UnixDevel Posted November 18, 2020 Report Share Posted November 18, 2020 4 hours ago, Dragos said: Daca vrei sa mergi in continuare pe structura pe care o ai in prezent, poti folosi regex sa-ti extragi datele si dupa sa lucrezi cu ele, spre exemplu <?php $mysql = mysqli_connect("localhost","ips","8hLXKFoFBuLld9LW","tests") or die("connection failure"); $q = $mysql->query("select val from ips"); $ips = array(); while($v = $q->fetch_row()) { preg_match('/((?:[0-9]{1,3}\.){3}[0-9]{1,3}) ([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]) ([0-2]?[0-9]\/[0-5]?[0-9]\/[0-9][0-9][0-9][0-9])/', $v[0], $matches); // scoatem prin regex IP-ul, ora si data din randul de mysql // echo $matches[1]; // IP // echo $matches[2]; // Ora // echo $matches[3]; // Data $ips[] = $matches[1]; // pusham IP-ul din randul de mysql in array-ul de IPs } print_r(array_count_values($ips)); // afisam de cate ori apare fiecare IP Cu valorile urmatoare in baza de date 127.0.0.1 13:29:05 13/08/2020 123.123.123.123 13:29:05 13/08/2020 127.0.0.1 13:29:05 13/08/2020 scriptul returneaza Array ( [127.0.0.1] => 2 [123.123.123.123] => 1 ) Chestia asta o sa coste mult cand vine vb de resurse Quote Link to comment Share on other sites More sharing options...
WIK Posted November 18, 2020 Author Report Share Posted November 18, 2020 3 hours ago, Dragos said: Daca vrei sa mergi in continuare pe structura pe care o ai in prezent, poti folosi regex sa-ti extragi datele si dupa sa lucrezi cu ele, spre exemplu <?php $mysql = mysqli_connect("localhost","ips","8hLXKFoFBuLld9LW","tests") or die("connection failure"); $q = $mysql->query("select val from ips"); $ips = array(); while($v = $q->fetch_row()) { preg_match('/((?:[0-9]{1,3}\.){3}[0-9]{1,3}) ([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]) ([0-2]?[0-9]\/[0-5]?[0-9]\/[0-9][0-9][0-9][0-9])/', $v[0], $matches); // scoatem prin regex IP-ul, ora si data din randul de mysql // echo $matches[1]; // IP // echo $matches[2]; // Ora // echo $matches[3]; // Data $ips[] = $matches[1]; // pusham IP-ul din randul de mysql in array-ul de IPs } print_r(array_count_values($ips)); // afisam de cate ori apare fiecare IP Cu valorile urmatoare in baza de date 127.0.0.1 13:29:05 13/08/2020 123.123.123.123 13:29:05 13/08/2020 127.0.0.1 13:29:05 13/08/2020 scriptul returneaza Array ( [127.0.0.1] => 2 [123.123.123.123] => 1 ) si acest array poate fi pus intr-un "if" sa se faca verificare daca un IP se repeta de 2 ori si unde mai exact se repeta ? Quote Link to comment Share on other sites More sharing options...
Moderators Dragos Posted November 18, 2020 Moderators Report Share Posted November 18, 2020 Sunt multe modalitati de a optimiza codul pe care l-am pus. Scriptul e scris in graba si testat doar pe un batch mic de elemente. 1 Quote Link to comment Share on other sites More sharing options...
gigiRoman Posted November 18, 2020 Report Share Posted November 18, 2020 2 hours ago, Dragos said: Sunt multe modalitati de a optimiza codul pe care l-am pus. Scriptul e scris in graba si testat doar pe un batch mic de elemente. Il loc de array poti pune direct intr-un dictionary... key value. Quote Link to comment Share on other sites More sharing options...