这是我第一次使用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
任何得到极大赞赏的帮助
答案 0 :(得分:1)
在你的模型方法中,你只是运行查询而不是在你的db-&gt; get()put row()或者结果方法获取记录之后才获取记录,只有单个记录结果才会得到记录的对象作为数组也在你我做了一点修改
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;
}