Execute mysql query with variable from php webscript

I have to give rank to users based on scores stored in db table. For that I prepared query and working fine from MySql workbench. Here it is,

SET @rank=0;
        UPDATE pm_user pmu inner join 
            (SELECT user_id, (@rank := IF(@score <> total_score, @rank + 1, @rank)) rank, 
            (@score := total_score) total_score FROM pm_user ORDER BY total_score DESC) var 
            on var.user_id = pmu.user_id
        set 
            pmu.country_rank = var.rank

But now I have to execute this query from php webscript, for which I tried to execute it like this,

<?php
    error_reporting(E_ERROR);
    include("db_connection.php");

// Udate user ranks
    $query = mysql_query("SET @rank=0");
    $query = mysql_query("UPDATE pm_user pmu inner join 
            (SELECT user_id, (@rank := IF(@score <> total_score, @rank + 1, @rank)) rank, 
            (@score := total_score) total_score FROM pm_user ORDER BY total_score DESC) var 
            on var.user_id = pmu.user_id
        set 
            pmu.country_rank = var.rank");

?>

But it didn’t update anything in db table.
Am I going right. Or it should be executed differently?


Source: mysql

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.