0

这是我第一次使用 MVC,并且我已经阅读了代码点火器的文档。但我似乎无法正确查询:

$score = $_POST['time'];
$sql = "SELECT COUNT(score) as c FROM highscores WHERE score < '$score' ORDER BY score ASC LIMIT 10";
$result = mysqli_query($con, $sql);
$count = mysqli_fetch_object($result)->c;
if(!is_null($count) && $count < 10) {
    echo 1;
}

然后是我的代码点火器版本:

public function insert_highscore($score) {
    $this->db->select('COUNT(score) as c')->from('highscores')->where('score < ' . $score);
    $query = $this->db->get();
    $count = $query->c;
    if(!is_null($count) && $count < 10) {
        echo 1;
    }
    return $count;
}

我这样称呼那个函数:

public function index()
{
    $this->load->view('header');
    $this->load->model('highscores_model');
    $data = $this->highscores_model->get_highscores();
    $this->highscores_model->insert_highscore("00:00:01.11");
    foreach($data as $highscore){
        echo $highscore['name'];
        echo $highscore['score'];
    }
    $this->load->view('footer');
}

并得到这个错误

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':00:01.11' at line 3

SELECT COUNT(score) as c FROM (`highscores`) WHERE `score` < 00:00:01.11

Filename: /storage/websites/cambist_app/codeigniter/models/highscores_model.php

Line Number: 15

非常感谢任何帮助

4

1 回答 1

0

在您的模型方法中,您只是在运行查询,但在您的 db->get() 之后不获取记录 put row() 或 result 方法来获取查询行的记录将只获得单个记录结果将获取记录对象作为数组也在你在那里我做了一点修改

public function insert_highscore($score) {
    $this->db->select('COUNT(score) as c')->from('highscores')->where('score < ' , $score);
    $query = $this->db->get()->row();
    $count = $query->c;
    if(!is_null($count) && $count < 10) {
        echo 1;
    }
    return $count;
}

您可以使用 count_all_results 来获取所有记录

 public function insert_highscore($score) {
        $this->db->where('score < ' , $score);
        $count = $this->db->count_all_results('highscores');
        if(!is_null($count) && $count < 10) {
            echo 1;
        }
        return $count;
    }
于 2013-06-09T16:58:04.977 回答