Hey all,
I am having an issue, when I run my query (shown below) directly in MS SQL it returns the first row (home.php / 323 hits), along with the rest.
Query:
SELECT TOP 20 Target, COUNT(Target) as Hits FROM InternetLog WHERE (Target LIKE '%.php' or Target LIKE '%.htm%') AND (Target NOT LIKE '%index%') AND (Target NOT LIKE '%weather%') AND (Target NOT LIKE '%Console%') GROUP BY Target ORDER BY Hits DESC
When I put it into PHP, it skips the first row..
<?php
require $_SERVER['DOCUMENT_ROOT']."/include/mainfunc.inc.php";
function print_r_html($data,$return_data=false)
{
$data = print_r($data,true);
$data = str_replace( " "," ", $data);
$data = str_replace( "\r\n","<br>\r\n", $data);
$data = str_replace( "\r","<br>\r", $data);
$data = str_replace( "\n","<br>\n", $data);
if (!$return_data)
echo $data;
else
return $data;
}
function getArrCount ($arr, $depth=1) {
if (!is_array($arr) || !$depth) return 0;
$res=count($arr);
foreach ($arr as $in_ar)
$res+=getArrCount($in_ar, $depth-1);
return $res;
}
$conn = connect_db('IIS');
$query = " SELECT TOP 20 Target, COUNT(Target) as Hits FROM InternetLog WHERE (Target LIKE '%.php' or Target LIKE '%.htm%') AND (Target NOT LIKE '%index%') AND (Target NOT LIKE '%weather%') AND (Target NOT LIKE '%Console%') GROUP BY Target ORDER BY Hits DESC";
$result = mssql_query($query);
$pq = mssql_fetch_array($result,MSSQL_ASSOC);
$finished = array();
$fcount = 0;
$count = 0;
/*
// This returns the *same* results as below...
for ($i = 0; $i < mssql_num_rows( $result ); ++$i)
{
$line = mssql_fetch_row($result);
print( "$line[0] - $line[1]\n");
}
*/
while ($row = mssql_fetch_object($result)) {
// echo $row->Target;
$x = explode('/',$row->Target);
// echo getArrCount($x);
$p = (getArrCount($x) - 1);
echo "File: ".$x[$p]." Hits: $row->Hits<br>";
$count = $count + $row->Hits;
$finished[$fcount] = array($x[$p],$row->Hits);
$fcount++;
}
echo "Total: $count Hits<br><br>";
//print_r_html($finished);
print_r_html($pq);
?>
The Query works perfect, and when I print_r the $pq (which is mssql_fetch_array) it has the first row (only).
Instead of attempting to use two functions, what am I doing wrong? the $row= statement should work correctly!
Here is an idea of my output:
File: bleh.php Hits: 63
File: partlistings.php Hits: 63
File: cbleh.php Hits: 44
File: anotherfile.php Hits: 40
File: training.php Hits: 34
File: view.php Hits: 31
File: casjs.html Hits: 28
File: manuals.php Hits: 26
File: vacation.html Hits: 25
File: blank.php Hits: 24
File: training1.php Hits: 24
File: search-results.php Hits: 22
File: processess.php Hits: 21
File: blank1.php Hits: 21
File: help.html Hits: 21
File: ordanalysis.php Hits: 19
File: MobileHome.html Hits: 17
File: warranty.php Hits: 16
File: ziplookup.php Hits: 16
Total: 555 Hits
// Note: below is the output of print_r($pq);
Array
(
[Target] => /techcc/home.php
[Hits] => 323
)
Any ideas what I'm doing wrong?