【问题标题】:CodeIgniter Select Statement with Where clause带有 Where 子句的 CodeIgniter Select 语句
【发布时间】:2014-07-04 02:33:35
【问题描述】:

嗨,我是 CodeIgniter 的新手,我只想知道如何从 MySql Db 中查询,这是一个带有 where 子句的 Select 语句,我知道它可以从网上搜索到,但是每当我尝试某些东西时,我都会出错, 真是令人沮丧。 Where 子句中的字符串将来自用户输入。多谢你们!

【问题讨论】:

  • 你能分享一些你尝试过的代码吗?这些东西都应该包含在 CI 手册中
  • $expiry = $this->db->query("SELECT abc FROM abc_db WHERE abc_id = '$abc'"); $expiry2 = $expiry->results();
  • 我调用未定义的方法 CI_DB_mysql_result::results()
  • 抱歉问题各位
  • 因为没有results()只有result()请参考文档ellislab.com/codeigniter/user-guide/database/index.html

标签: php mysql sql codeigniter


【解决方案1】:

您可以按照 Mehedi-PSTU 所述进行操作,但是您似乎对此有点陌生,所以这里有一些额外的信息:

我将在这里复制 Mehedi-PSTU 的大部分内容。

$this->get->where('column_name', $equals_this_variable);
$query = $this->db->get('table_name');

这会将查询对象存储在变量 $query 中。 如果您想将其转换为可用的数组,您只需执行以下操作。

$results = $query->result_array();

或者你可以像这样循环遍历它:

foreach($query->result_array() as $result){
    // Perform some task here.
}

更好甚至更全面的理解可能来自:

http://ellislab.com/codeigniter/user-guide/database/active_record.html

【讨论】:

  • 问题@Craig Barben 如果我选择一个特定的column_name,而不是所有等于$equals_this_variable 的行怎么办?喜欢
  • select name from table_name where id = '$id' like this how in CI?
  • 添加 $this->db->select('column_name');在 where 语句之前。
  • 数组到字符串的转换
【解决方案2】:

试试这样的

    $this->db->where('db_attr', $var);
    return $this->db->get('table');

【讨论】:

    【解决方案3】:

    试试这个。

    $id = 'your id';
    $this->db->select("*");
    $this->db->from("table_name");
    $this->db->where('id','$id');
    $query = $this->db->get();
    return $query->result_array();
    

    【讨论】:

      【解决方案4】:

      在具有方法链接样式的 Codeigniter 中:-

       $data['getData'] = $this->db->get_where('table_name',array('column_name'=>$var))->result_array();
      

      【讨论】:

        猜你喜欢
        • 2021-03-14
        • 2018-03-08
        • 1970-01-01
        • 2018-06-15
        • 2016-03-25
        • 2022-08-15
        • 2010-12-07
        • 1970-01-01
        • 2013-07-07
        相关资源
        最近更新 更多