Codeigniter从表中选择单行

ryhaxcpt  于 2023-03-06  发布在  其他
关注(0)|答案(3)|浏览(89)

嗨,我的数据库中有一个名为news的表,我想使用querybuilder只获取特定id的新闻。我写了这段代码,但在视图中没有结果。

$this->db->from("news");
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notizie'] =$q->result_array();

如果我打印查询的numrow()方法,我可以看到有一行,但如果我为运行此方法,则没有文本

foreach($notizie as $notizia)
{
    echo $notizia["titoloit"];
}
gr8qqesn

gr8qqesn1#

哦这个问题问了这么久,从评论中我可以看出错误其实是
错误编号:1066 0x066非唯一表/别名:'news' SELECT * FROM新闻,新闻WHERE id ='1'文件名:controllers/News.php
错误来自数据库。请查看生成的查询SELECT * FROM news, news:有两个"新闻",使数据库混乱。2试着删除第一行。

// $this->db->from("news"); No you dont need this with get_where
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notize'] =$q->result_array();

$notizie = $q->result_array();
if($q->num_rows()>0){
  foreach($notizie as $notizia)
  {
     echo $notizie["titoloit"];
  }
}
jk9hmnmh

jk9hmnmh2#

$this->db->from("news");
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notize'] =$q->result_array();

$notizie = $q->result_array();
if($q->num_rows()>0){
  foreach($notizie as $notizia)
  {
     echo $notizie["titoloit"];
  }
}

这个应该可以。在你粘贴的代码中foreach里面的变量是$notizia,我猜你是指$notizie对吗?

lnvxswe2

lnvxswe23#

型号

$this->db->from("news");
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notize'] =$q->result_array();
$this->load->view('your_viewFIleName' , $data);

应该将数据传递给视图控制器

相关问题