php 错误:类 CI_DB_mysql_result 的对象无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15931716/
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
Error : Object of class CI_DB_mysql_result could not be converted to string
提问by Septian Rizky
I'm new to CodeIgniter, I've tried to read the documentation of CI but I still can't solve my problem, maybe someone here can help fix my problem. Here is my code:
我是 CodeIgniter 的新手,我试图阅读 CI 的文档,但我仍然无法解决我的问题,也许这里有人可以帮助解决我的问题。这是我的代码:
In my controller
在我的控制器中
class Registration extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('registration_model','rmod');
}
function ambil() {
$gender = $this->input->post('kelamin');
$tinggi = $this->input->post('height');
$berat = $this->input->post('weight');
$weight = $this->rmod->ambilBeratPria($tinggi);
echo $weight;
}
In my model
在我的模型中
function ambilBeratPria($tinggi) {
$this->db->select('berat')->from('pria')->where('tinggi',$tinggi);
$query = $this->db->get();
return $query;
}
I want to get the result of my query in the model, but i get an error like this:
我想在模型中获取我的查询结果,但出现如下错误:
Message: Object of class CI_DB_mysql_result could not be converted to string
Message: Object of class CI_DB_mysql_result could not be converted to string
Maybe someone here can help to solve my problem ? Thanks.
也许这里有人可以帮助解决我的问题?谢谢。
回答by Yan Berk
You need to return the result of the query:
您需要返回查询的结果:
function ambilBeratPria($tinggi) {
$this->db->select('berat')->from('pria')->where('tinggi',$tinggi);
$query = $this->db->get();
return $query->result();
}
EDIT:
编辑:
If the result is a single row:
如果结果是单行:
function ambilBeratPria($tinggi) {
$this->db->select('berat')->from('pria')->where('tinggi',$tinggi);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row()->berat;
}
return false;
}
回答by jleft
Currently you're trying to directly echo the value returned by $this->db->get();
. However, to use the result(s) from the query, you need to generate the results.
目前,您正在尝试直接回显$this->db->get();
. 但是,要使用查询的结果,您需要生成结果。
If you generate the query like this:
如果您生成这样的查询:
$query = $this->db->get();
Then there are several options for generating results. These examples assume that you have a column in the row(s) being returned called weight.
然后有几种生成结果的选项。这些示例假设您在返回的行中有一个名为weight 的列。
result()
- Allows you to use the results as an array of objects.
result()
- 允许您将结果用作对象数组。
if ($query->num_rows() > 0) //Ensure that there is at least one result
{
foreach ($query->result() as $row) //Iterate through results
{
echo $row->weight;
}
}
result_array()
- Allows you to use the results as an array.
result_array()
- 允许您将结果用作数组。
if ($query->num_rows() > 0) //Ensure that there is at least one result
{
foreach ($query->result_array() as $row) //Iterate through results
{
echo $row['weight'];
}
}
row()
- If you're expecting only one result then this can be useful. If the query generates multiple results, then only the first rowis returned. Allows you to use the result as an object.
row()
- 如果您只期待一个结果,那么这可能很有用。如果查询生成多个结果,则仅返回第一行。允许您将结果用作对象。
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->weight;
}
row_array()
- The same as row()
but allows you to use the result as an array.
row_array()
- 与row()
但允许您将结果用作数组相同。
if ($query->num_rows() > 0)
{
$row = $query->row_array();
echo $row['weight'];
}
回答by SithuSena
I got the same error when I was working in a PHP project with Code Igniter framework.In there in my model class there was a method called getprojectnames(),
我在使用 Code Igniter 框架的 PHP 项目中工作时遇到了同样的错误。在我的模型类中有一个名为 getprojectnames() 的方法,
public function getprojectnames(){
$result['names']=array();
$this->db->select('name');
$this->db->from('project');
$this->db->where('status','Not Completed');
$query=$this->db->get();
return $query->result();
}
I wanted to call this function in the controller class and use it in a drop down list in the view class.
我想在控制器类中调用这个函数并在视图类的下拉列表中使用它。
So in my controller class,
所以在我的控制器类中,
$this->data['projects'] =$this->testcase_model->getprojectnames();
$this->load->view("admin/_layout_main",$this->data);
In my view class,
在我的视图类中,
<?php echo form_open('admin/check1'); ?>
<div class="row" style=" margin-left: 10px; margin-top: 15px;">
<h5 class="box-title">Projects List : <? php echo form_dropdown('projects', $projects, 'id'); ?> </h5>
<div>
<input type="submit" style="width:200px;" class="btn btn-block btn-primary" value="Show Project TestCase Status" /></div>
</div>
<?php echo form_close();?>
When I run this I got the error,telling CI_DB_mysql_result could not convert into String.So I solved this problem by changing my code in the model class like as follows,
当我运行它时,我得到了错误,告诉 CI_DB_mysql_result 无法转换为字符串。所以我通过如下更改模型类中的代码解决了这个问题,
public function getprojectnames(){
$result['names']=array();
$this->db->select('name');
$this->db->from('project');
$this->db->where('status','Not Completed');
$query=$this->db->get();
foreach ($query->result() as $row)
{
array_push($result,$row->name);
}
return $result;
}
Then my program worked fine.
然后我的程序运行良好。