Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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
Php Codeigniter返回数字列上的单行_Php_Mysql_Codeigniter - Fatal编程技术网

Php Codeigniter返回数字列上的单行

Php Codeigniter返回数字列上的单行,php,mysql,codeigniter,Php,Mysql,Codeigniter,所以我有一张如下表: Day | 2 | 4 | 6 |8 |10 | 12 | 14 | 16| ------------------------------------------- 3/12/2014| 0 | 0 | 1 |1 | 1 | 1 | 0 | 1 | 我的问题是 SELECT `14` FROM (`Time_clock`) WHERE `Day` = '03/12/2014' 我的查询返回0,这是正确的。 但是我无法绑定并返回得到的结果未定义属性:std

所以我有一张如下表:

Day      | 2 | 4 | 6 |8 |10 | 12 | 14 | 16| 
-------------------------------------------
3/12/2014| 0 | 0 | 1 |1 | 1 | 1  | 0  | 1 |
我的问题是

SELECT `14` FROM (`Time_clock`) WHERE `Day` = '03/12/2014'
我的查询返回0,这是正确的。

但是我无法绑定并返回得到的结果
未定义属性:stdClass::$14

当我尝试执行以下操作时,我只需要单行结果[0]:

    $this->db->select($current_hour);
    $this->db->from('Time_clock');
    $this->db->where('Day',$Todays_Date);
    $query = $this->db->get();      
            if ($query->num_rows() > 0)
            {
            $row = $query->row(); 
            return $row->$current_hour; //error in this line
            }
是因为我的列标题是数字吗?

更改

return $row->$current_hour; 

那第二块钱把你搞砸了


**而且它是区分大小写的,因此,从db**

列名称中精确匹配列标题numeric绝对是一个好主意,因为以数值开头的标识符无效,当您试图获取对象属性(如
$row->$current\u hour
)时,会出现错误,其中
$current\u hour
包含属性名称,即数值及其无效的属性名称。但您仍然可以通过如下更改代码来获取数据

在select上添加“`”以保护mysql的标识符,就像CI看到它的数字一样,它认为您在select上传递了一个默认值,因此它忽略标识符保护,您也将始终将列名作为值

    $this->db->select("`$current_hour`");
    $this->db->from('Time_clock');
    $this->db->where('Day', $Todays_Date);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        $row = $query->row_array(); // get as array 
        return $row[$current_hour]; //user array index instant of object property 
    }
强烈
建议在您的表格列中添加前缀,即
hour_2,hour_3,…


并像这样使用
$this->db->select(“hour_$current_uhour”)

更好的设计可能是这样的

Date       | Hour | Flag
2014-12-03 |  2   |   0
2014-12-03 |  4   |   0
2014-12-03 |  6   |   1
2014-12-03 |  8   |   1
2014-12-03 | 10   |   1
2014-12-03 | 12   |   1
2014-12-03 | 14   |   0
2014-12-03 | 16   |   1
…或者这个

Datetime              |  Flag
2014-12-03 02:00:00   |   0
2014-12-03 04:00:00   |   0
2014-12-03 06:00:00   |   1
2014-12-03 08:00:00   |   1
2014-12-03 10:00:00   |   1
2014-12-03 12:00:00   |   1
2014-12-03 14:00:00   |   0
2014-12-03 16:00:00   |   1
请尝试以下命令:

SELECT 14 FROM table_name WHERE date(Day)="'2014/03/02'"

现在我得到了:
未定义的属性:stdClass:$current\u hour
@Deverbe2k我更新了答案尝试一下,别忘了它的大小写敏感请参见规范化,是的-不要使用整数作为列标识符。@草莓我用军事时间来记录一天中的小时数,这就是为什么列标题是数字的,我看不出有什么问题。@草莓,你能建议一种更好的设置方法吗?
SELECT 14 FROM table_name WHERE date(Day)="'2014/03/02'"