Jump to content
WIK

PHP : sa caute doua valori din baza de date

Recommended Posts

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 ?

 

 

Link to comment
Share on other sites

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 by Wav3
  • Upvote 1
Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

  • Moderators

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 by Dragos
  • Upvote 2
Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...