Php 如何使用codeigniter在数据库中不存在数据时显示空数据

Php 如何使用codeigniter在数据库中不存在数据时显示空数据,php,mysql,database,codeigniter,activerecord,Php,Mysql,Database,Codeigniter,Activerecord,我正在连接两个表,我想在两个表的id相等时显示数据。当id不相等时,什么也不显示,但它显示了一切 这是我的控制器 <?php /** * */ class profile extends CI_Controller { function __construct() { parent::__construct(); } function index() { $this->load->model("p

我正在连接两个表,我想在两个表的id相等时显示数据。当id不相等时,什么也不显示,但它显示了一切

这是我的控制器

<?php

/**
* 
*/
class profile extends CI_Controller
{

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

    function index()
    {
         $this->load->model("profile_model");
         $data['profile_records']=$this->profile_model->getAllRecords();

    }

}

?>

这是我的模型

<?php

/**
* 
*/
class profile_model extends CI_Model
{

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

    function getAllRecords()
    {


        $this->db->select('*');
        $this->db->from('ads AS A');// I use aliasing make things joins easier
        $this->db->join('membership AS C', 'C.id = A.ad_id', 'INNER');
        $this->db->where('A.ad_id  = C.ad_fk'); 

             $q = $this->db->get("ads");
            if($q->num_rows() > 0)
            {
               return $q->result();
            }
            return array();
    }
}
?> 

这是我的看法

<?php




foreach ($profile_records as $profile_rows)
{
?>

<span>


    echo "<span class='title2'>". $profile_rows->ad_timestamp ."</span>"."<br/>";?>
    <?php  echo "<span class='title2'>". $profile_rows->ad_title ."</span>"."<br/>";?>
    <?php  echo "<span class='title'>". $profile_rows->ad_description ."</span>"."<br/>";?>
    <?php  echo "<span class='title2'>". $profile_rows->cat_type ."</span>"."<br/>";?>
    <?php  echo "<span class='title2'>". $profile_rows->ad_name ."</span>"."<br/>";?>
    <?php  echo "<span class='description'>". $profile_rows->ad_phone ."</span>"."<br/>";?>
    <?php  echo "<span class='name'>". $profile_rows->ad_address ."</span>"."<br/>";?>
    <?php  echo "<span class='phone'>". $profile_rows->ad_flag."</span>"."<br/>";?>
    <?php  //echo "<span class='website'>". $row->address ."</span>"."<br/>";?>
</span>
<hr width="600px" />
<?php }?>

“回声”$配置文件行->广告时间戳。“
”;?>
据我所知,如果值为空,您希望打印null吗?这是代码

在您的模型中,尝试将此代码替换为

$q = $this->db->get("ads");
        if($q->num_rows() > 0)
        {
           return $q->result();
        }
        return false;
在你看来

if(!$profile_records)
{
    //the value is null
   echo "The value is null";
}
else
{
    foreach ($profile_records as $profile_rows)
    {
    ?>

       <span>

         echo "<span class='title2'>". $profile_rows->ad_timestamp ."</span>"."<br/>";?>
         <?php  echo "<span class='title2'>". $profile_rows->ad_title ."</span>"."<br/>";?>
         <?php  echo "<span class='title'>". $profile_rows->ad_description ."</span>"."<br/>";?>
         <?php  echo "<span class='title2'>". $profile_rows->cat_type ."</span>"."<br/>";?>
         <?php  echo "<span class='title2'>". $profile_rows->ad_name ."</span>"."<br/>";?>
         <?php  echo "<span class='description'>". $profile_rows->ad_phone ."</span>"."<br/>";?>
         <?php  echo "<span class='name'>". $profile_rows->ad_address ."</span>"."<br/>";?>
         <?php  echo "<span class='phone'>". $profile_rows->ad_flag."</span>"."<br/>";?>
         <?php  //echo "<span class='website'>". $row->address ."</span>"."<br/>";?>
     </span>
     <hr width="600px" />
  <?php }
} ?>
if(!$profile\u记录)
{
//该值为空
回显“值为空”;
}
其他的
{
foreach($profile\u记录为$profile\u行)
{
?>
echo“$profile\u行->广告时间戳。”“
”;?>
听起来像是一个MySQL
join
问题,虽然我不了解实际问题,但您能否从您希望加入的表中包含一些示例数据和期望的结果?请告诉我们您得到了什么结果,以便更好地帮助您,因为您在评论中附加的任何内容都不起作用。@Muhammad,请复制图片请仔细查看我的代码我正在连接两个表。您的代码工作不正常我知道您的连接表,我建议的模型中的代码是正确的。错误在哪里我的条件是当两个id相等时显示数据,或者当id不相等时显示数据l或者不存在,所以什么也不显示,但是您的代码打印my db中可用的所有内容,因为您在模型中删除了此代码,$this->db->select('*');$this->db->from('ads AS A');//我使用别名使连接更容易$this->db->join('membership AS C',C.id=A.ad_id',INNER');$this->db->where('A.ad_id=C.ad_fk');在该代码之后插入我建议在sql中使用该语句的代码,以确定需要显示的结果