PutraKg
PutraKg

Reputation: 2246

How do I get the result into a variable MYSQL

I have the following PHP script. How do I get the correct results. Each query should only return a number. I need to get rank in this case. I was also wondering how to combine those two queries into one statement.

function getRankings($country, $deviceid)
{

$queryWorld = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score ) AS rank FROM
ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";

$queryCountry = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score  AND Country='$country') AS rank FROM ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";

$resultWorld =  mysql_query($queryWorld) or die(mysql_error());  
$rowWorld = mysql_fetch_row($resultWorld);
$resultCountry =  mysql_query($queryCountry) or die(mysql_error());
$rowCountry = mysql_fetch_row($resultCountry);

$arr = array();
$arr[] = array("WorldRanking" => $rowWorld[0], "CountryRanking" => $rowCountry[0]);
echo json_encode($arr);         
}

If I type the queries individually into MYSQl I get the correct answers. But the echo produces

[{"WorldRanking":null,"CountryRanking":null}]

It should be something like

[{"WorldRanking":"4","CountryRanking":"1"}]

I think I need to get the value of rank but I do not know how.

Upvotes: 0

Views: 44

Answers (2)

Vipin Kumar Soni
Vipin Kumar Soni

Reputation: 834

<?php
function getRankings($country, $deviceid)
{

$queryWorld = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score ) AS rank FROM
ScoreTable b WHERE DeviceID=$deviceid ORDER BY rank LIMIT 1";

$queryCountry = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score  AND Country='$country') AS rank FROM ScoreTable b WHERE DeviceID=$deviceid ORDER BY rank LIMIT 1";

$resultWorld =  mysql_query($queryWorld) or die(mysql_error());  
$no_of_results_in_resultWorld = mysql_num_rows($resultWorld);
if($no_of_results_in_resultWorld > 0){
    $rowWorld = mysql_fetch_row($resultWorld);
}

$resultCountry =  mysql_query($queryCountry) or die(mysql_error());
$no_of_results_in_resultCountry = mysql_num_rows($resultCountry);
if($no_of_results_in_resultCountry > 0){
    $rowCountry = mysql_fetch_row($resultCountry);
}

$arr = array();
$arr[] = array("WorldRanking" => $rowWorld[0], "CountryRanking" => $rowCountry[0]);
echo json_encode($arr);         
}

check whether it is returning results or not.

Upvotes: 0

user3462511
user3462511

Reputation:

try your code like this, but I don't know this code correct or not:

function getRankings($country, $deviceid)
{

$queryWorld = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score ) AS rank FROM
ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";

$queryCountry = "SELECT 1 + (SELECT count( * ) FROM ScoreTable a WHERE a.score > b.score  AND Country='$country') AS rank FROM ScoreTable b WHERE DeviceID='$deviceid' ORDER BY rank LIMIT 1";

$resultWorld =  mysql_query($queryWorld) or die(mysql_error());  
while ($rowWorld = mysql_fetch_row($resultWorld)){
$value1 = $rowWorld['filedname1'];
}
$resultCountry =  mysql_query($queryCountry) or die(mysql_error());
while($rowCountry = mysql_fetch_row($resultCountry)){
$value2 = $rowWorld['filedname2'];
}

$arr[] = array("WorldRanking" => $value1, "CountryRanking" => $value2);
print_r($arr);        
}

Upvotes: 1

Related Questions