尝试从2个表codeigniter获取数据时出错

时间:2017-04-05 14:00:04

标签: php mysql codeigniter fetch

我要从2个表中获取数据..我的表是“学习”,“用户”和“主题”

  

“Study”包括:(id,user_id [是表“Users”的列“id”的外键],subject_id [是表“id”列的外键“科目“],年级,日期)

     

“用户”包括:(ID,用户名,姓名,姓氏,密码,类型,状态,日期)

     

“主题”包括:(id,career_id,名称,描述,小时)

我想在最后得到这样的东西:

enter image description here

我收到了这个错误:

enter image description here

enter image description here

enter image description here

这是我的代码: 我的查看文件(“主页”):

    <html>

    <head>



    </head>

<body>

    <div class="container"> 
    <div class="row">
    <div class="col-md-12">

        <h2 align="center">TABLE:Study</h2>

        <input id="busqueda_tabla" type="text">
            <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
                <thead>
                    <th>id</th>
                    <th>User</th>
                    <th>Subject</th>
                    <th>Grade</th>
                    <th>Date</th>
                    <th>Action</th>
                </thead>

<tbody>
    <?php

    if (count($records) > 0 && $records != false) {
        foreach($records as $record) {

            echo "<tr>
                      <td>".$record['id']."</td>
                      <td>".$record['user']."</td>
                      <td>".$record['subject']."</td>
                      <td>".$record['grade']."</td>
                      <td>".$record['date']."</td>
                      <td align='center'>

                         <button type='button' class='btn btn-primary'>EDITAR</button></a> |

                         <button type='button' class='btn btn-danger'>BORRAR</button></a>

                  </tr>";
        }

       }
    ?>

</tbody>

    </table>

        </div>
        </div>
        </div>

</body>
</html>

这是我的Controller文件(“Home”):

    <?php

    class Home extends CI_Controller{

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

             $this->load->model("Crudmodel");

        }


        public function index(){

    # get all data in Study table
    $selectStudys = $this->Crudmodel->selectStudys();

    foreach ($selectStudys as $key => $study) 
    {
        # get UserNames
        $user = $this->Crudmodel->getName($study['user_id']);

        #get Subject Names
        $subject = $this->Crudmodel->getSubName($study['subject_id']);


        #append both NEW VALUES to same array


        $data[$key]['user_id'] = $user[0]['username'];
        $data[$key]['subject_id'] = $subject[0]['name'];

    }


    $data['records'] = $selectStudys;
    $this->load->view('home', $data);

}



   }

?>

我的模型文件(“Crudmodel”):

  <?php

    class Crudmodel extends CI_Model{

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

         $this->load->database();

        }


        function selectStudys()
{
    $query= $this->db->query("SELECT * FROM Study");
    $result = $query->result_array();
    return $result;
}

function getName($name)
{
    $query= $this->db->query("SELECT username FROM Users WHERE id = $name ");
    $result = $query->result_array();
    return $result;
}

function getSubName($subject)
{
    $query= $this->db->query("SELECT name FROM Subjects WHERE id = $subject ");
    $result = $query->result_array();
    return $result;
}








}
?>

希望你能帮助我:/

2 个答案:

答案 0 :(得分:0)

未定义的索引和尝试获取属性非对象或多或少意味着同样的事情是您没有获得正确的数据或者您尝试获取的变量或索引未初始化或未定义且导致此错误可能是错误在查询或空白数据返回查询您正在运行。

我想请你提取这样的查询数据

$check = $this->db->query("SELECT * FROM SOMETHING AND YOUR CONDITION AND STUFF HERE"); 

if($check->num_rows()>0){
 $result = $check->result(); 
 return $result;
}else{
 return false; // or anything you want. 
}

假设这个查询函数存储在模型中,你就像这样调用你的模型

$someVariable = $this->model_name->function();
if($someVariable!==FALSE){
// handle
}else 
{
// handle
}

最后,不知道为什么,但我也有时候用双引号来解决问题,我知道......双引号内的变量工作,我只是说某个时候......至少它发生在我身上,所以我想要求最后一件事。尝试像这样调试你的查询,目前你有

"SELECT * FROM TABLE WHERE THIS = $THAT" 

将此更改为

"SELECT * FROM TABLE WHERE THIS = '".$THAT."'"

我希望它能帮到你!

<强>编辑: (很抱歉,我没有从您自己的代码中显示示例) 您的模型文件

  <?php

    class Crudmodel extends CI_Model{

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

         $this->load->database();

        }


        function selectStudys()
{
    $query= $this->db->query("SELECT * FROM Study");
    if($query->num_rows()>0){
       $result = $query->result_array();
     }else{
      $result = "";
          // or anything you can use as error handler
      return $result;
    }
}

function getName($name)
{
    $query= $this->db->query("SELECT username FROM Users WHERE id = $name ");
    if($query->num_rows()>0){
    $result = $query->result_array();
    }else{
    $result = "";
          // or anything you can use as error handler
    return $result;
  }
}

function getSubName($subject)
{
    $query= $this->db->query("SELECT name FROM Subjects WHERE id = $subject ");
  if($query->num_rows()>0){
     $result = $query->result_array();
  }else{
      $result = "";
          // or anything you can use as error handler
      return $result;
}
    }

 function CombineResults($subject, $name){
  // you can also use this
   $query = $this->db->query("SELECT sub.name, user.username FROM Subjects sub, Users user WHERE sub.id=$subject AND user.id = $name");
   if($query->num_rows()>0){
        return $query->result();
         }else{
           return "";
              // or anything you can use as error handler
        }
    }       
  }
    ?>

您的控制器文件

  public function index(){

    # get all data in Study table
    $selectStudys = $this->Crudmodel->selectStudys();
    // we have condition on this model method/function we can validate
    // response comming from this method and add handler just like we did
    // for queries. your main problem can be this

    foreach ($selectStudys as $key => $study) 
    {
        # get UserNames
        $user = $this->Crudmodel->getName($study['user_id']);

        #get Subject Names
        $subject = $this->Crudmodel->getSubName($study['subject_id']);


        #append both NEW VALUES to same array

        if(!empty($user[0]['username'])){
        $data[$key]['user_id'] = $user[0]['username'];
        // your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
        }else{
         $data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler 
        }
        if(!empty($subject[0]['name'])){
        $data[$key]['subject_id'] = $subject[0]['name'];
        // your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
        }else{
           $data[$key]["subject_id"] = "";
          // or anything you can use as error handler
        }

    }


    $data['records'] = $selectStudys;
    $this->load->view('home', $data);

}

答案 1 :(得分:0)

我将您的查询更改为加入查询,只需将您的代码更改为

public function index(){

    # get all data in Study table

    $query = $this->db->query("SELECT sd.user_id as id,sd.grade as grade,sd.date as date,sd.subject_id as subject,ur.username as user FROM Study as sd,Users as ur,Subjects as sb WHERE ur.id=sd.user_id and sb.id=sd.subject_id");

    $result = $query->result_array();

    $data['records'] = $result;

    $this->load->view('home', $data);

}

现在运行代码