Php Codeigniter从talbe中选择单行
您好,我的数据库中有一个名为news的表,使用querybuilder,我只想获取具有特定id的新闻。我编写了此代码,但在视图中没有得到任何结果Php Codeigniter从talbe中选择单行,php,mysql,codeigniter,Php,Mysql,Codeigniter,您好,我的数据库中有一个名为news的表,使用querybuilder,我只想获取具有特定id的新闻。我编写了此代码,但在视图中没有得到任何结果 $this->db->from("news"); $q=$this->db->get_where('news', array('id' => $idNotizia)); $data['notizie'] =$q->result_array(); 如果我打印查询的numrow方法,我可以看到有一行,但是如果我运行这个
$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"];
}
这应该行得通。在粘贴的代码中,foreach内部的变量是$notizia,我猜您的意思是$notizie,对吗 模型
$this->db->from("news");
$q=$this->db->get_where('news', array('id' => $idNotizia));
$data['notize'] =$q->result_array();
$this->load->view('your_viewFIleName' , $data);
您应该将数据传递给视图控制器哦,这个问题持续了很长时间。 从评论中我可以看出错误实际上是 错误号:1066非唯一表/别名:“news”从news中选择*news,其中id='1'文件名:controllers/news.php 错误来自数据库。查看生成的查询SELECT*fromnews,news:有两条新闻,这使数据库很混乱。尝试删除第一行
// $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"];
}
}
这和我写的代码是一样的。它不起作用。不,我的意思是$notizia,就像手册中的解释一样,当您使用codeigniterSorry时,能否正确添加您在模型、视图和控制器中所做的工作,第一部分在控制器中制作,第二部分在视图中制作,在我传递数据数组的地方,其中包含变量“notizie”在视图中打印的结果是什么?$notize我可以在视图文件中看到拼写错误。查询结果已分配到$data['notize']变量中,您正在foreach循环中使用$notizie。请交叉检查,让我们知道它是否工作。很抱歉,这是问题中的一个类型错误。我现在修复了。代码不起作用,我得到了错误Not unique table/alias:'news'我以前做过,但我得到了错误号:1066 Not unique table/alias:'news'SELECT*FROM news,news其中id='1'文件名:controllers/news.php
// $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"];
}
}