【问题标题】:Undefined Variable query in codeignitercodeigniter中的未定义变量查询
【发布时间】:2014-01-26 12:13:31
【问题描述】:

我是代码点火器的新手

我在尝试创建排行榜时收到此错误

遇到了 PHP 错误

严重性:通知

消息:未定义变量:查询

文件名:models/status_model.php

行号:19

我的代码看起来像这样

<?php
class status_model extends CI_Model
{
function get_leaderboard(){
    //prepare a leaderboard
    $data = array();

    $details = array(
        'rank' => NULL,
        'fb_name' => NULL,
        'level' => NULL,
        'college' => NULL
    );

    $rank = 1;
    
    $sql = "SELECT fb_name, level, college, role FROM users ORDER BY level DESC, passtime ASC"; 
    $query = $this->db->query($sql,$start*50);
    if ($query->num_rows() > 0)
    
        foreach ($query->result() as $row)
        {
            //Only regular users are shown in the leaderboard
            //banned users and admins have a rank 0, and are excluded.
            if($row->role == 1){

                $details['rank'] = $rank;
                $details['fb_name'] = $row->fb_name;
                $details['level'] = $row->level;
                $details['college'] = $row->college;
                array_push($data, $details);

                $rank++;
            }
        }
        return $data;
    } else {
        //couldn't find any rows!?
        return false;
    }
    
}

function get_rank($fb_uid=''){
    //calculate rank of the current user, or given fb_uid

    if($fb_uid == ''){
        $fb_uid = $this->session->userdata('facebook_uid');
    }

    if($fb_uid=='' || $fb_uid==NULL){
        return 0;
    }

    //make sure the uid corresponds to a regular user
    $sql = "SELECT fb_uid, role FROM users WHERE fb_uid = $fb_uid"; 
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0)
    {
        $row = $query->row();
        $role = $row->role;
    }else{
        return 0;
    }

    if($role!=1){
        //Rank is 0 for anyone other than a regular user.
        return 0;
    }

    //count from 0 to the current user's position
    $rank = 0;

    $sql = "SELECT fb_uid FROM users WHERE role=1 ORDER BY level DESC, passtime ASC"; 
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $rank++;
            if($row->fb_uid == $fb_uid){
                return $rank;
            }
        }
    }

    return 0;

}

function get_winners(){
    //For future use, if winner's details are 
    //added to database

    //return an empty array for now.
    $data = array();
    return $data;
}
}

我的 HTML 看起来像这样

    <?php
    $list='';

    foreach($leaderboard as $row){
            $list.="<tr>";
            $list.="<td>".$row['rank']."</td>";
            $list.="<td>".$row['fb_name']."</td>";
            $list.="<td>".$row['level']."</td>";
            $list.="<td>".$row['college']."</td>";
            $list.="</tr>";
        }

    }

    ?>
    <table class="table table-striped table-hover">
        <thead>
            <tr>
                <th>Rank #</th>
                <th>Username</th>
                <th>Level</th>
                <th>College</th>
            </tr>
        </thead>
        <tbody>
            <?=$list?>
        </tbody>
    </table>
</div>

【问题讨论】:

  • 你能直接显示你的问题行吗?为什么是整个代码?
  • 什么是行号:19 ??
  • 变量$start 未定义。 @echo_Me 跟踪错误通常更容易,因为在这种情况下,如果您粘贴更多内容,那么他​​得到错误的那一行实际上没有错误。
  • @echo_Me 我得到#start undefined
  • @Emz 我该怎么做?或者我现在应该怎么做我很新

标签: php mysql database codeigniter


【解决方案1】:

我对您的问题有一些疑问。您在 foreach 中使用 if 而不是在查询中使用 WHERE 子句。您不使用 db 类中内置的 CodeIgniters 的任何原因?您可以使用 $this-&gt;db-&gt;select('fb_name'...)-&gt;where()-&gt;order_by()-&gt;get(); 而不是像 $sql = "SELECT fb_name, level, college, role FROM users WHERE rank=$rank ORDER BY level DESC, passtime ASC"; 这样的查询

有些用户不喜欢链接它们,而是在不同的步骤中执行 $this->db。

我推荐浏览Correct naming structure for CodeIgnitor这个url,看看在CodeIgniter中应该如何命名

此外,这应该可以工作,使其更高效和更清洁,它只获取排名为 1 的查询。然后从中获取结果,如果有超过 0 个结果则生成结果。

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’); 
class status_model extends CI_Model {

    function get_leaderboard(){

        $sql = "SELECT fb_name, level, college, role FROM users WHERE rank=1 ORDER BY level DESC, passtime ASC"; 

        $query = $this->db->query($sql);

        if ($query->num_rows() > 0)
            $data = $query->result();
            return $data;
        } else {

            //couldn't find any rows!?
            return false;
        }
    }
...

【讨论】:

    【解决方案2】:

    你为什么不那样使用它

       $query = $this->db->query($sql);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多