Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Bom pessoal estou desenvolvendo um sistema aqui na empresa para controle de acesso a internet... ele esta pronto so que preciso da ajuda de vocês ja que eu nunca fiz isso antes....
bom tenho uma tabela:
traffic:
Id, Date, Time, Ip, ResultCode, Byte, Url, Outhuser, UsersiteId.
Em traffic Ip e um numero qualquer 97234972834. id e a chave primaria.
tabela 2:
hostnames:
Id, Ip, Hostname
Em ip na tabela hostname fica o mesmo numero 97234972834. e o mesmo Id da tabela traffic. detalhe: hostname tem um ip valido no caso 192.168.0.2.... []
com isso a tabela compoe a outra....
minha duvida como eu faço para ordernar isso no php ?
minha linha de codigo
$query = "SELECT * FROM trafficSummaries ORDER BY id";
no caso ficaria com select em mais de uma tabela mas como ?? ordenado por Id vai ?tipo assim exemplo:
tabela1 temos id dados nome fornecedor
tabela2 temos id host nfornecedor
tabela 1 vou precisar de id dados nome so fornecedor que nao... ja que o mesmo gera um numero estranho que eu acho que tem algo a ver com o nfornecedor que ai sim tem um numero que bate com a sequencia padrao do numero de fornecedor.
entao eu precisaria imprimir na tela para o usuario apenas os campos id , dados , host, nfornecedor...
--------------
acima foi so um exemplo.... talvez eu postando a parte do meu codigo que trata sobre isso possa ajudar...
function fetch_records($table_name, $fields){
include 'conexao.php';
include 'execulta_sql.php';
// Fazendo uma consulta SQL e retornando os resultados em uma tabela HTML id, date, userID, inCache, outCache, sitesID, summaryTime
//$query = "SELECT a.id, a.date, a.time, a.bytes, a.url, a.authuser, b.id, b.hostnames FROM traffic a, hostnames b WHERER a.id = b.id"; $query = "SELECT 'traffic'.id, 'traffic'.date, 'traffic'.time, 'traffic'.bytes, 'traffic'.url, 'traffic'.authuser, 'hostnames'.id, 'hostnames'.hostnames FROM traffic, hostnames WHERER traffic.id = hostnames.id";
$query = "SELECT LTRIM(id), LTRIM(date), LTRIM(time), LTRIM(bytes), LTRIM(url), LTRIM(authuser) FROM traffic ORDER BY authuser";
$resultado = mysql_query($query,$conexao);
if($resultado)
{
if(mysql_num_rows($resultado) == 0){
return "nao a dados no seu database";
}
Else {
while($arr = mysql_fetch_array($resultado, MYSQL_ASSOC)){
$table .= "\t\t<tr>\n";
foreach ($arr as $val_col)
{
$table .= "\t\t\t".'<td>'.$val_col.'</td>'."\n";
}
$table .= "\t\t</tr>\n";
}
$table .= "</table>";
return $table;
}
mysql_free_result($run);
}
//while ($linha = mysql_fetch_array($resultado))
return "erro ao criar tabela.";
}
{
agora as tabelas que eu preciso....
tabela traffic:
id date time ip resultCode bytes url authuser sitesID usersID
1 2010-05-28 11:40:53 3232235757 TCP_MISS/200 654 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.25521790 - 1 1
2 2010-05-28 11:40:53 3232235757 TCP_MISS/200 379 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.1576145976 - 1 1
3 2010-05-28 11:40:54 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.83185023 - 2 1
4 2010-05-28 11:40:55 3232235761 TCP_MISS/200 85732 banklineplus.itau.com.br:443 mlucia 3 2
5 2010-05-28 11:41:04 3232235757 TCP_MISS/200 436 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.2093793087 - 2 1
6 2010-05-28 11:41:14 3232235757 TCP_MISS/200 380 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.586927027 - 1 1
7 2010-05-28 11:41:14 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.279417638 - 2 1
8 2010-05-28 11:41:15 3232235757 TCP_MISS/200 72757 http://sup.live.com/whatsnew/whatsnewservice.asmx - 4 1
9 2010-05-28 11:41:21 3232235761 TCP_MISS/200 25381 banklineplus.itau.com.br:443 mlucia 3 2
10 2010-05-28 11:41:24 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.1406180507 - 2 1
11 2010-05-28 11:41:33 3232235757 TCP_MISS/200 379 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.2095337437 - 1 1
12 2010-05-28 11:41:34 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.1042846779 - 2 1
13 2010-05-28 11:41:44 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.1721769905 - 2 1
14 2010-05-28 11:41:53 3232235757 TCP_MISS/200 380 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.864853970 - 1 1
15 2010-05-28 11:41:54 3232235757 TCP_MISS/200 436 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.1663084660 - 2 1
16 2010-05-28 11:42:04 3232235757 TCP_MISS/200 436 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.705897682 - 2 1
17 2010-05-28 11:42:05 3232235724 TCP_MISS/200 10000 local-bay.contacts.msn.com:443 - 5 1
18 2010-05-28 11:42:13 3232235757 TCP_MISS/200 380 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.1594356451 - 1 1
19 2010-05-28 11:42:14 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.145723675 - 2 1
20 2010-05-28 11:42:24 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.2020471750 - 2 1
21 2010-05-28 11:42:33 3232235757 TCP_MISS/200 380 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.1828802264 - 1 1
22 2010-05-28 11:42:34 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.1836118448 - 2 1
23 2010-05-28 11:42:44 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.1447880617 - 2 1
24 2010-05-28 11:42:53 3232235757 TCP_MISS/200 380 http://65.54.50.41/gateway/gateway.dll?Action=poll&SessionID=1916541178.1407795825 - 1 1
25 2010-05-28 11:42:54 3232235757 TCP_MISS/200 437 http://65.54.49.73/gateway/gateway.dll?Action=poll&SessionID=1518209777.1255246065 -
ip = e um numero nada a ver com ip....
tabela hostname:
id ip description isResolved hostname
1 3232235757
0 192.168.0.237
2 3232235761
0 192.168.0.241
3 3232235724
0 192.168.0.204
4 3232235719
0 192.168.0.199
5 3232235755
0 192.168.0.235
6 3232235695
0 192.168.0.175
7 3232235561
0 192.168.0.41
8 3232235620
0 192.168.0.100
9 1914739316
0 114.32.154.116
10 3232235607
0 192.168.0.87
11 3232235642
0 192.168.0.122
12 3232235722
0 192.168.0.202
13 3232235675
0 192.168.0.155
14 3232235759
0 192.168.0.239
15 3232235645
0 192.168.0.125
16 3232235673
0 192.168.0.153
17 3232235637
0 192.168.0.117
18 3232235738
0 192.168.0.218
19 3232235599
0 192.168.0.79
20 3232235590
0 192.168.0.70
21 3232235743
0 192.168.0.223
22 3232235610
0 192.168.0.90
23 3232235754
0 192.168.0.234
24 3232235753
0 192.168.0.233
25 3232235697
0 192.168.0.177
agora sim aqui ip e um valor real... esse dado que eu quero... junto com a primeira tabela.... id e a chave primaria...
Quando você junta duas tabelas, é necessário especificar a tabela.campo a qual se refere, a menos que defina para ele um alias:
SELECT * FROM `tabela1` JOIN `tabela2` ON `tabela1`.`id` = `tabela2`.`id` ORDER BY `tabela1`.`nome`
SELECT `a`.*, `b`.`nome` AS `nomeB` FROM `tabela1` AS `a` JOIN `tabela2` AS `b` ON `a`.`id` = `b`.`id` ORDER BY `nomeB`
Lembrando que o alias não pode ser igual a um nome de campo já existente.
beleza pessoal olha consegui resolver obrigado vcs me ajudaram muito !!!... ficou assim entao...
meu select:
$query = "SELECT trafficSummaries.*, hostnames.hostname
FROM trafficSummaries,hostnames
WHERE trafficSummaries.date B
ETWEEN '2010-05-28' AND '2010-05-28'";
so tem um problema cosmico... minha pesquisa esta demorando muito !! tem como dar um bosster nessa pesquisa ?? ela esta ultrapassando o limite do php....
e outra coisa preciso que na pesquisa tmbm seja feito com nome do usuario tmbm.... entao eu fiz assim ... mas nao se esta certo ou tem uma maneira melhor...
$query = "SELECT trafficSummaries.*, hostnames.hostname
FROM trafficSummaries,hostnames
WHERE trafficSummaries.date B
ETWEEN '2010-05-28' AND '2010-05-28' AND trafficSummaries.data LIKE '%celio.ribeiro%'";
Célio.. boa tarde
Explica melhor o que você quer...
Se entendi certo você precisa relacionar as duas tabelas.
Você disse que o campo IP é o mesmo nas duas tabelas... OK!
Disse também que ID tb é o mesmo ? Porque ? cada tabela tem que ter a sua chave primária.
Para relacionar as duas tabelas faça:
SELEC * FROM
traffic A
JOIN hostnames B ON A.IP = B.IP
Para ordenar você coloca o apelido da tabela e o campo:
ORDER BY A.ID -- ORDERNA PELO ID DA TABELA traffic
-- OU
ORDER BY B.ID -- ORDERNA PELO ID DA TABELA hostnames
A SQL completa fica:
SELEC * FROM
traffic A
JOIN hostnames B ON A.IP = B.IP
ORDER BY A.ID -- ORDERNA PELO ID DA TABELA traffic
Espero que ajude.
abs
Ricardo