将 codeigniter 查询转换为 json?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2412574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert codeigniter query to json?
提问by marko
I want to convert a model query to json with json_encode, it doesn't work. But with a ordinary array it does.
我想使用 json_encode 将模型查询转换为 json,它不起作用。但是对于普通数组,它确实如此。
$arr = array("one", "two", "three");
$data["json"] = json_encode($arr);
Output
输出
<?php echo "var arr=".$json.";"; ?>
var arr=["one","two","three"];
But when I try to convert a query codeigniter throws an error. What is it with that? This is the error message:
但是当我尝试转换查询时,codeigniter 会引发错误。那是怎么回事?这是错误消息:
A PHP Error was encountered Severity: Warning Message: [json] (php_json_encode) type is unsupported, encoded as null
遇到 PHP 错误严重性:警告消息:[json] (php_json_encode) 类型不受支持,编码为空
And the converted "query" result = I mean model method is like this:
转换后的“查询”结果 = 我的意思是模型方法是这样的:
{"conn_id":null,"result_id":null,"result_array":[],"result_object":[],"current_row":0,"num_rows":9,"row_data":null}
I try to do like this
我试着这样做
$posts = $this->Posts_model->SelectAll();
$data["posts"] = json_encode($posts);
By the way, the model and method works just fine when I do it without json_encode.
顺便说一句,当我没有 json_encode 时,模型和方法工作得很好。
Something I'm propably doing wrong, but the question is what?
我可能做错了什么,但问题是什么?
回答by Stephen Curran
You appear to be trying to encode the CodeIgniter database result object rather than the result array. The database result object acts as a wrapper around the cursor into the result set. You should fetch the result array from the result object and then encode that.
您似乎正在尝试对 CodeIgniter 数据库结果对象而不是结果数组进行编码。数据库结果对象充当将游标包裹到结果集中的包装器。您应该从结果对象中获取结果数组,然后对其进行编码。
Your model code appears to be something like this :
您的模型代码似乎是这样的:
function SelectAll()
{
$sql = 'SELECT * FROM posts';
// Return the result object
return $this->db->query($sql);
}
It should be more like this :
它应该更像这样:
function SelectAll()
{
$sql = 'SELECT * FROM posts';
$query = $this->db->query($sql);
// Fetch the result array from the result object and return it
return $query->result();
}
This will return an array of objects which you can encode in JSON.
这将返回一个可以用 JSON 编码的对象数组。
The reason you are getting an error trying to encode the result object is because it has a resourcemember variable that cannot be encoded in JSON. This resource variable is actually the cursor into the result set.
您在尝试对结果对象进行编码时遇到错误的原因是它有一个无法用 JSON 编码的资源成员变量。这个资源变量实际上是进入结果集的游标。
回答by bamossza
public function lastActivity()
{
header("Content-Type: application/json");
$this->db->select('*');
$this->db->from('table_name');
$query = $this->db->get();
return json_encode($query->result());
}
回答by Jai Narayan Singh
As per latest CI standard use the following code in your controller file:
根据最新的 CI 标准,在您的控制器文件中使用以下代码:
$this->output->set_content_type('application/json')->set_output(json_encode($arr));
回答by Ajay Malhotra
Here is the working solution:
这是工作解决方案:
$json_data = $this->home_model->home_getall();
$arr = array();
foreach ($json_data as $results) {
$arr[] = array(
'id' => $results->id,
'text' => $results->name
);
}
//save data mysql data in json encode format
$data['select2data'] = json_encode($arr);
回答by Pro RZ
Models (Post):
型号(后):
function SelectAll()
{
$this->db->select('*');
$this->db->from('post');
$query = $this->db->get();
return $query;
}
Controllers :
控制器:
$data['post'] = $this->post->SelectAll()->result_array();
echo json_encode($data);
Result:
结果:
{"post":[{"id":"5","name":"name_of_post"}]}

