简体   繁体   中英

Message: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

The following does not work and throws an exception.

leaderboard_mode.php

<?php

class Leaderboard_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    public function get_by_role()
    {
        $query = $this->db->query->("SELECT b. * FROM (SELECT a. *, @rank := @rank + 1 rank FROM (SELECT MAX(score) AS max_score, user_id FROM highscore WHERE game_mode="bungkata" GROUP BY user_id ORDER BY max_score DESC)a, (SELECT @rank := 0)r)b LIMIT 10");
        return $query->result();
    }
}

This is the exception:

An uncaught Exception was encountered

Type: ParseError

Message: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

Filename: C:\\wamp\\www\\leaderboard\\application\\models\\Leaderboard_model.php

Line Number: 12

Backtrace:

File: C:\\wamp\\www\\leaderboard\\application\\controllers\\Leaderboard.php
Line: 9
Function: model

File: C:\\wamp\\www\\leaderboard\\index.php
Line: 315
Function: require_once

You just need to remove -> from query-> , like $this->db->query() Please see below updated code

public function get_by_role()
    {
        $query = $this->db->query("SELECT b. * FROM (SELECT a. *, @rank := @rank + 1 rank FROM (SELECT MAX(score) AS max_score, user_id FROM highscore WHERE game_mode='bungkata' GROUP BY user_id ORDER BY max_score DESC)a, (SELECT @rank := 0)r)b LIMIT 10");
        return $query->result();
    }

change this query $query = $this->db->query-> to $query = $this->db->query("your sql query")

public function get_by_role()
{
    $query = $this->db->query->("SELECT b. * FROM (SELECT a. *, @rank := @rank + 1 rank FROM (SELECT MAX(score) AS max_score, user_id FROM highscore WHERE game_mode="bungkata" GROUP BY user_id ORDER BY max_score DESC)a, (SELECT @rank := 0)r)b LIMIT 10");
    return $query->result();
}

leaderboard_model.php

 <?php class Leaderboard_model extends CI_Model { function __construct() { parent::__construct(); } public function get_by_role() { $query = $this->db->query("SELECT b. * FROM (SELECT a. *, @rank := @rank + 1 rank FROM (SELECT MAX(score) AS max_score, user_id FROM highscore WHERE game_mode=bungkata GROUP BY user_id ORDER BY max_score DESC)a, (SELECT @rank := 0)r)b LIMIT 10"); return $query->result(); } } 

controller : leaderboard.php

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Leaderboard extends CI_Controller { function __construct() { parent::__construct(); $this->load->model(array('Leaderboard_model')); } public function index(){ $data['datarank'] = $this->leaderboard_model->get_by_role(); $this->load->view('content_home', $data); } } 

my views: content_home.php

 <div id="main-description" class="col-md-6"> <div class="heading text-center"> <h1 class="text-white wow fadeInLeft"><strong>LEADERBOARD</strong></h1> </div> <div class="table-responsive text-white wow fadeInLeft"> <table id="authusertable" class="table table-bordered table-striped table-hover"> <thead> <tr> <th>Peringkat</th> <th>Nama</th> <th>Point</th> </tr> </thead> <tbody> <?php $no = 0; foreach ($datarank as $row): $no++ ?> <tr> <td><?=$no?> <td><?=$row->user_id?></td> <td><?=$row->max_score?></td> </tr> <?php endforeach; ?> </tbody> <tfoot> </tfoot> </table> </div> </div> 

image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM