我的Codeigniter上有数据库错误

时间:2018-02-21 16:46:34

标签: codeigniter

当我尝试登录我的项目时收到以下错误,请帮助

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `_id` = '1'' at line 2

SELECT * WHERE `_id` = '1'

Filename: views/backend/header.php

Line Number: 34

............................................... ............................     这是头文件的代码行 ..........................

<?php
$name = $this->db->get_where($this->session->userdata('login_type'),
    array($this->session->userdata('login_type').'_id' => $this->session->userdata('login_user_id')))
    ->row()
    ->name;
echo $name;

发生数据库错误

Error Number: 1054

Unknown column '_id' in 'where clause'

UPDATE `ci_sessions` SET `timestamp` = 1519230907 WHERE `_id` = '1' AND `id` = 'd25c0dcdaa9a86810d791b05ba53fe45b76a7bcd'

Filename: libraries/Session/drivers/Session_database_driver.php

Line Number: 243

2 个答案:

答案 0 :(得分:0)

您没有定义从哪个表中获取数据,这就是出现此错误的原因。通常我们编写像

这样的查询
SELECT * FROM `table_name` WHERE `_id` = 1

你只是错过了选择表。在codeigniter形式中,查询应该是

$this->db->select('*');
$this->db->from('table_name');
$this->db->where('_id', '1');
$data = $this->db->get();

这是在codeigniter中获取数据的正常过程。

答案 1 :(得分:0)

最好手动添加列名和表名。并且值存储在变量

$id = $this->session->userdata('login_user_id'); 
$name = $this->db->get_where('table-name',array('column-name' => 'your-value'))->row();
$echo $name->column-name; //Using this code you can echo all your data retrieved from database