Jump to content

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


Go to solution Solved by thatguywhoasksquestions,

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();
    }
Edited by thatguywhoasksquestions
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.