php 选择最大代码点火器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8910671/
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
select max codeigniter
提问by Augus
Im trying to get an max value with codeigniter from an table but it isnt working. This is the error i get:
我试图从表中使用 codeigniter 获取最大值,但它不起作用。这是我得到的错误:
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: database/DB_active_rec.php
Line Number: 427
严重性:4096
消息:类 CI_DB_mysql_result 的对象无法转换为字符串
文件名:数据库/DB_active_rec.php
行号:427
This is my function:
这是我的功能:
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$result = $this->db->get('rapporten');
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
$data = $query + 1;
return $data;
}
What im trying to do is as followed:
我试图做的是如下:
- Select the highest
id
wherebedrijf_id
=$bedrijf_id
fromrapporten
. - Select the
periode_nummer
fromstatistieken_onderhoud
whererapporten_id
= the highestid
i got from step 1. - Add 1 to the
periode_nummer
i got from step 2 andreturn
that number.
- 选择最高的
id
wherebedrijf_id
=$bedrijf_id
fromrapporten
。 - 选择
periode_nummer
fromstatistieken_onderhoud
whererapporten_id
=id
我从步骤 1 中得到的最高值。 - 将 1 添加到
periode_nummer
我从第 2 步得到的return
数字和那个数字上。
Thanks in forward for your help!
感谢您的帮助!
回答by stealthyninja
Try
尝试
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$res1 = $this->db->get('rapporten');
if ($res1->num_rows() > 0)
{
$res2 = $res1->result_array();
$result = $res2[0]['id'];
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
if ($query->num_rows() > 0)
{
$row = $query->result_array();
$data['query'] = 1 + $row[0]['periode_nummer'];
}
return $data['query'];
}
return NULL;
}
回答by Santosh Pandey
Try this:
尝试这个:
$this->db->select_max('display_sequence');
$this->db->from('acl_menu');
$query = $this->db->get();
$r=$query->result();
Display Sequence is your column name & acl_menu is your table name.
Display Sequence 是您的列名,acl_menu 是您的表名。
回答by Maicol Romero
$this->db->select_max('id', 'max_id');
$query = $this->db->get('video_processing');
return $query->row();
try the above:
试试上面的:
回答by mirza
You can't use an object as a string. Use this:
您不能将对象用作字符串。用这个:
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$result = $this->db->get('rapporten');
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
// fetch first row in object
$result = $query->row();
$data = $result + 1;
return $data;
}
回答by gustyaquino
I think the $query
variable is holding a mysql result resource and it cannot be used as a String or in this case an Integer.
我认为该$query
变量保存了一个 mysql 结果资源,它不能用作字符串或在这种情况下是整数。
You could try this way:
你可以试试这样:
$data = mysql_result($query,0) + 1;