Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter活动记录返回类型_Codeigniter_Activerecord - Fatal编程技术网

Codeigniter活动记录返回类型

Codeigniter活动记录返回类型,codeigniter,activerecord,Codeigniter,Activerecord,我尝试使用此SQL查询获取umat\u id: SELECT umat_id FROM msumat WHERE nama = $nama 我已将此SQL查询转换为CI的活动记录: $this->db->select('umat_id'); $terdaftar = $this->db->get_where('msumat', array('nama' => $nama)); 所以这个查询应该返回一个字符串(例如:“John”) 但我在尝试回显时出错: 类CI\

我尝试使用此SQL查询获取
umat\u id

SELECT umat_id FROM msumat WHERE nama = $nama
我已将此SQL查询转换为CI的活动记录:

$this->db->select('umat_id');
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
所以这个查询应该返回一个字符串(例如:“John”)

但我在尝试回显时出错: 类CI\u DB\u mysql\u结果的对象无法转换为字符串

我试过这样的方法:
echo(string)$terdaftar,但它不工作

我只想回应“约翰”

编辑 刚才说我想在变量中插入“John”。怎么做

$john = ????
尝试:

阅读以了解更多信息。

尝试以下方法:

 $this->db->select('umat_id');
 $terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
 $row = $terdaftar->row_array();
 $your_variable = $row['umat_id']; /*Here comes your john*/

由于一些用户已经指出了解决方案,我只是解释为什么会出现此错误,以便更好地理解codeigniter提供的查询结果

此错误:

但当我尝试回显它时,我得到了一个错误:类的对象 无法将CI_DB_mysql_结果转换为字符串

发生这种情况是因为您试图回显一个对象

这段代码

$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
将返回一个对象,此对象将包含有关您所做查询的信息。 使用此对象,可以将结果(行)作为执行以下操作的对象:

$results = $terdaftar->result();
$results = $terdaftar->result_array();
$number_results = $terdaftar->num_rows()
或者,如果您对数组更满意,可以将结果(行)作为数组返回,执行以下操作:

$results = $terdaftar->result();
$results = $terdaftar->result_array();
$number_results = $terdaftar->num_rows()
您还可以通过以下方式获得结果的数量:

$results = $terdaftar->result();
$results = $terdaftar->result_array();
$number_results = $terdaftar->num_rows()
这只是一个例子,你可以在这里阅读更多关于结果的信息

编辑 更好的解释是:假设我们使用result_array()函数以纯数组格式获取结果:

$results = $terdaftar->result_array();
现在,变量$results是一个数组,要遍历它并获得所需的数据,需要执行以下操作:

foreach ($results as $key => $row) {
    //the row variable will have each row of your database returned by your query
    //so if you want to access a field from that row, 
    //let's say for example the name field. You would do something like this
    if($row['name']=='John')
        echo $row['name'];

}

感谢您的帮助:D,请查看我编辑的问题(不在文档中):D非常感谢您的帮助:我已更新了我的答案,以便您更好地了解如何从一行中获取特定字段