Shandhanu Mohan
Shandhanu Mohan

Reputation: 123

calculating percentile for huge database

i have huge database containing student information about test code and the marks achieved for those test code. I need to recompute percentile marks for students corresponding to each test codes. I have a code for a series of test code but it doesn't work properly.

function recompute_percentiles()
{
if($_REQUEST[testcode]=="CAT B1" or $_REQUEST[testcode]=="CAT B2" or $_REQUEST[testcode]=="CAT B3" or $_REQUEST[testcode]=="CAT B4")
{

echo "<br />Got testcode: ".$_REQUEST[testcode];


$getsortedq=mysql_query("SELECT username, section1right as m from kmarks where testcode='.$_REQUEST[testcode].' order by section1right DESC");

if(!$getsortedq) 
echo "Could not get the sorted query";
else 
echo "got the sorted query quick";



$totalcount=mysql_num_rows($getsortedq);

while($r=mysql_fetch_array($getsortedq))
{
$u=$r[username];
$m=$r[m];
$array[$u]=$m;
}



$array2=$array;
//print_r($array2);

$updated=0;

foreach($array as $key=>$value)
{
$countsame=0;
foreach($array2 as $k=>$v)
{
    if($v>=$value) 
    $countsame++; 
    else
    break;
}
$countless = $totalcount - $countsame;

reset($array2);

$percentile=round($countless/$totalcount*100,2);

$updatep1q=mysql_query("UPDATE kmarks set percentile1=$percentile where   username='.$key.' and testcode='.$_REQUEST[testcode].'");

if(!$updatep1q)
  echo "<br />Could not update p1 for username: ".$key;
else
    $updated++;


}

echo "<br />Updated ".$updated." records in kmarks db, out of ".$totalcount." records for  testcode ".$_REQUEST[testcode];




}
}

Upvotes: 1

Views: 831

Answers (2)

user5236399
user5236399

Reputation:

Seems like a lot of code. You could do something like this:

$results = $db->query("SELECT * FROM your_table ORDER BY sort_field");

$data = array();
while($row = $results->fetch_assoc()){
    $data[] = $row;
}

$chunks = array_chunk($data,ceil((count($data)/100)));
foreach($chunks as $key => $dataset){ 
$percentile = 99 - $key;

    foreach($dataset as $row){
        $db->query("UPDATE your_table SET percentile={$percentile} WHERE id={$row['id']}");
    }
}

Upvotes: 0

ppeterka
ppeterka

Reputation: 20726

There are multiple , serious issues with this code - without even touching the functionality...

1 PHP syntax

$_REQUEST[testcode]

Not good, always use braces!

$_REQUEST['testcode']

2 Injection proneness

You are wide open to SQL Injection, and HTML/Javascript injection too

echo "<br />Got testcode: ".$_REQUEST[testcode]; //HTML injection...
//SQL injection
$getsortedq=mysql_query("SELECT username, section1right as m from kmarks where testcode='.$_REQUEST[testcode].' order by section1right DESC"); 

Always use proper sanitization (mysql(i)_real_escape_string($_REQUEST['testcode']) depending on mysql_ or mysqli being used). Or even better: prepared statements in the SQL case...

3 Deprecation

The Obligatory mysql_* Warning: mysql_ functions are deprecated as of PHP 5.5. Don't use them: use either PDO or at least mysqli_ functions...

Functionality

This is the culprit:

$updatep1q=mysql_query("UPDATE kmarks set percentile1=$percentile where   username='.$key.' and testcode='.$_REQUEST[testcode].'");

The resulting query will read:

UPDATE kmarks set percentile1=<somevalue>  --this is OK
where username='.<somevalue>.' and testcode='.$_REQUEST[testcode].'
                ^           ^                ^^^^^^^^^^^^^^^^^^^^^

The problems are highlighted... There are unwanted dots, and a whole bad part. I suppose you wanted something like this

UPDATE kmarks set percentile1=<somevalue>  
where username='<somevalue>' and testcode='<somevalue>'

Use it like this instead (of course with sanitization!!!):

//WARNING! STILL HAS SQL INJECTION --apply sanitization from #2 to make it safer...
$updatep1q=mysql_query("UPDATE kmarks set percentile1=$percentile where username='".$key."' and testcode='".$_REQUEST[testcode]."'");

Arrays can't be used inside string literals, and . concatenation operators are not required in case of plain variables...

Upvotes: 2

Related Questions