尝试从 2 个表 codeigniter 中获取数据时出错
Error trying to fetch data from 2 tables codeigniter
我必须从 2 个表中获取数据。我的表是“Study”、“Users”和“Subjects”
"Study" includes:(id, user_id[is the foreign key to the column "id" of the table "Users"], subject_id[is the foreign key to the column "id" of the table "Subjects"], grade, date)
"Users" includes:(id,username,name,lastname,password,type,status,date)
"Subjects" includes:(id, career_id, name, description, hours)
我想在最后得到这样的东西:
我遇到了这个错误:
这是我的代码:
我的视图文件(“主页”):
<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>
这是我的控制器文件(“主页”):
<?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;
}
}
?>
希望你能帮助我:/
Undefined indexes 和 trying to get 属性 non-object 或多或少意味着同一件事,即你没有得到正确的数据,或者你试图获取的变量或索引未初始化或未定义,并且造成这种情况的原因可能是查询中的错误或空白数据 return 通过查询您是 运行。
我想请求你像这样提取你的查询数据
$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);
}
我已将您的查询更改为加入查询,只需将您的代码更改为以下内容
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);
}
现在 运行 代码
我必须从 2 个表中获取数据。我的表是“Study”、“Users”和“Subjects”
"Study" includes:(id, user_id[is the foreign key to the column "id" of the table "Users"], subject_id[is the foreign key to the column "id" of the table "Subjects"], grade, date)
"Users" includes:(id,username,name,lastname,password,type,status,date)
"Subjects" includes:(id, career_id, name, description, hours)
我想在最后得到这样的东西:
我遇到了这个错误:
这是我的代码: 我的视图文件(“主页”):
<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>
这是我的控制器文件(“主页”):
<?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;
}
}
?>
希望你能帮助我:/
Undefined indexes 和 trying to get 属性 non-object 或多或少意味着同一件事,即你没有得到正确的数据,或者你试图获取的变量或索引未初始化或未定义,并且造成这种情况的原因可能是查询中的错误或空白数据 return 通过查询您是 运行。
我想请求你像这样提取你的查询数据
$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);
}
我已将您的查询更改为加入查询,只需将您的代码更改为以下内容
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);
}
现在 运行 代码