条件 'WHERE' 上的子字符串错误 CodeIgniter
Substring error on the condition 'WHERE' CodeIgniter
我有数据,其中数据包含我的 ID 以制作关键字,我尝试将 ID 分开并获取 4 位样本 ID "PRNE4" 但是在我尝试制作脚本后显示错误?怎么了?
我的link
错误
Message: Invalid argument supplied for foreach()
我的样品ID
PRNE1
PRNE2
控制器
public function Index($) {
$id = $this->uri->segment(3);
$data = array (
'profile' => $this->M_model->get_profile($id));
$this->load->view('layout/wrapper', $data);
}
型号
function get_profile($id) {
$query = $this->db->select('*')
->from('tb_sample')
->where("(SUBSTRING(id_sample, 0, 4) = '$id')")
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
观看次数
<?php
$no = 1;
foreach ($profile as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->profile.'</td>';
echo '</tr>';
$no++;
} ?>
试试这个。
在控制器中
public function index() {
$id = $this->uri->segment(3);
$data['profile'] = $this->M_model->get_profile($id);
$this->load->view('layout/wrapper', $data);
}
模型中
function get_profile($id) {
$this->db->select('*')
$this->db->from('tb_sample')
$this->db->where("(SUBSTRING(id_sample, 5) = '$id')") // if PRNE4 => 4 (cut the string start for pos of 5)
return $this->db->get();
}
在视图中
<?php
$no = 1;
foreach ($profile->result() as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->profile.'</td>';
echo '</tr>';
$no++;
}
?>
SUBSTRING 医生说
For all forms of SUBSTRING(), the position of the first character in
the string from which the substring is to be extracted is reckoned as
1.
->where("(SUBSTRING(id_sample, 1, 4) = '$id')")
所以第一位是1,不是0。
同样在您的模型中,如果查询结果为空,则 get_profile 函数中没有 return。您可以按照其他答案中的建议 return $query
并在视图中使用对象,或者 return 如果要保留格式,则在函数末尾使用空数组:
function get_profile($id) {
$query = $this->db->select('*')
->from('tb_sample')
->where("(SUBSTRING(id_sample, 1, 4) = '$id')")
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
return array();
}
我有数据,其中数据包含我的 ID 以制作关键字,我尝试将 ID 分开并获取 4 位样本 ID "PRNE4" 但是在我尝试制作脚本后显示错误?怎么了?
我的link
错误
Message: Invalid argument supplied for foreach()
我的样品ID
PRNE1
PRNE2
控制器
public function Index($) {
$id = $this->uri->segment(3);
$data = array (
'profile' => $this->M_model->get_profile($id));
$this->load->view('layout/wrapper', $data);
}
型号
function get_profile($id) {
$query = $this->db->select('*')
->from('tb_sample')
->where("(SUBSTRING(id_sample, 0, 4) = '$id')")
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
观看次数
<?php
$no = 1;
foreach ($profile as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->profile.'</td>';
echo '</tr>';
$no++;
} ?>
试试这个。
在控制器中
public function index() {
$id = $this->uri->segment(3);
$data['profile'] = $this->M_model->get_profile($id);
$this->load->view('layout/wrapper', $data);
}
模型中
function get_profile($id) {
$this->db->select('*')
$this->db->from('tb_sample')
$this->db->where("(SUBSTRING(id_sample, 5) = '$id')") // if PRNE4 => 4 (cut the string start for pos of 5)
return $this->db->get();
}
在视图中
<?php
$no = 1;
foreach ($profile->result() as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->profile.'</td>';
echo '</tr>';
$no++;
}
?>
SUBSTRING 医生说
For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.
->where("(SUBSTRING(id_sample, 1, 4) = '$id')")
所以第一位是1,不是0。
同样在您的模型中,如果查询结果为空,则 get_profile 函数中没有 return。您可以按照其他答案中的建议 return $query
并在视图中使用对象,或者 return 如果要保留格式,则在函数末尾使用空数组:
function get_profile($id) {
$query = $this->db->select('*')
->from('tb_sample')
->where("(SUBSTRING(id_sample, 1, 4) = '$id')")
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
return array();
}