如何在codeigniter中检索列值?

hm2xizp9  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(277)

我正试图找回 config_value 基于codeigniter项目中的图所示的表中的config\u名称。连我都不知道从哪里开始。我在网上找到了这样的东西(作为例子)。

$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
$data = $q->result_array();

echo($data[0]['age']);

rmbxnbpk

rmbxnbpk1#

虽然功能相同,但与其他答案差别不大。您可以创建一个助手并包含此函数。仅当您一次只想获取一个配置项时才真正有用。

function config_item_db($key)  {

    $CI = & get_instance();
    $query = $CI->db->get_where('my_users_table', array('config_name' => $key));
    if ($query->num_rows() == 1) {
        return $query->row()->{$key};
    } else {
        return false;
    }

}
oalqel3c

oalqel3c2#

如果您想根据配置名称检索配置值,只需在select查询中添加一个where条件,就像我在这里给出的那样,输入您想要检索查询的任何配置名称,它就会打印同一行中的配置值。

$this->db->select('config_value');
$q = $this->db->get_where('my_users_table',array('config_name'=>'home_page'));
$row = $q->row_array();
echo $row['config_name']; // this will print index.php
eoxn13cs

eoxn13cs3#

这样做:

$this->db->where('config_name', 'Account_activation');
$q = $this->db->get('my_users_table');
/* if u r fetching one row use row_array instead of result_array*/
$row = $q->row_array();

echo $row['config_value'];

更多信息:https://www.codeigniter.com/user_guide/database/index.html

cygmwpex

cygmwpex4#

使用codeignator的get\u where()方法并在数组中传递select条件。例子:

$this->load->database();
$this->db->get_where('table_name', array('database_column_title' => $value1, 
'database_column_title' => $value2));

相关问题