Jump to content

Running query in PhpMyAdmin gets results; however, not in PHP


Recommended Posts

The purpose of the MySQL query is to go through each row in the highscores table, and for each row, it increments @pos by each row number. For example: @pos = row 1, @pos = row 2. That is the subquery. The main SELECT will then go through all the returned rows, find the row with the userid of the user in question, and return that rows position. It works perfectly fine when ran in phpMyAdmin, e.g:

 

http://puu.sh/6q5Gx.png

 

The query was ran on the user who is #1 on the highscores, and as you can see, returned his appropriate rank; however, in my PHP function, I don't seem to be getting any results.

    public function getRank($id){
        $query = $this->db->processQuery("
            SET @pos=0;
            SELECT pos FROM
            (
                SELECT h.`userid`,@pos:=(@pos+1) AS pos FROM `highscores` AS h ORDER BY `money_earned` DESC
            ) as x WHERE x.`userid` = ?
        ", array($id), true);
        
        return $query[0]['pos'];
    }

I even print_r()'ed the $query variable, and I got nothing but an empty array.

 

If this helps, here's processQuery:

    /*
     * @METHOD  processInsertQuery
     * @DESC    prepares a query for use, then runs it
     */
    
    public function processQuery($query, array $binds, $fetch = false) {
        $query_handle = $this->dbc->prepare($query);
        
        if(!$query_handle->execute($binds)){
            $error = $query_handle->errorInfo();
            echo $error[2];
        }
        
        //update insertId var
        $this->insertId = $this->dbc->lastInsertId();
        
        //incase we ever want to get the number of rows affected
        //we set our row_count variable to the number of rows affected
        $this->row_count = $query_handle->rowCount();
        
        if($fetch == true)
            return $query_handle->fetchAll();
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.