【问题标题】:Can we check in a MODEL [CODIGNITER] whether a query returns true or false and based on it perform different operations like if else statement我们能否在 MODEL [CODIGNITER] 中检查查询是否返回 true 或 false,并根据它执行不同的操作,如 if else 语句
【发布时间】:2011-11-30 16:28:44
【问题描述】:

我们能否在 MODEL [CODIGNITER] 中检查 mysql 查询是否返回 true 或 false 并根据请求执行 if else 语句

模型中的一个 sql 语句执行是否 sql 语句给出 num_rows>0 如果它给出了我需要执行一个函数 else 继续其他行

【问题讨论】:

  • 您能详细说明一下吗?问题不够清楚,抱歉。

标签: php mysql codeigniter


【解决方案1】:

当然,这是一个人为的例子。

class Blogmodel extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function blog_exists( $id )
    {
        $query = $this->db->query('SELECT * FROM BLOG WHERE BLOG.ID = '. $id);

        if( $query->num_rows() > 0 )
            return true; 
        else 
            return false; 
    }


}

【讨论】:

    【解决方案2】:

    代码的任何部分都没有进入控制器。您需要在模型中创建另一个函数,根据控制器函数的真假调用该函数。例如:- 在模型中:-

    <?php
    function xyz($flag){
    if(----)return true;
    else return false;
    function abc($flag){----};
    function ghi($flag){----};
    ?>
    

    在控制器中:-

    function call($var){
    if(xyz($var)==TRUE) abc($var);
    else ghi($var);
     ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2019-02-28
      • 2012-11-14
      • 2013-09-14
      • 1970-01-01
      相关资源
      最近更新 更多