我想根据登录用户查看mysql
中codeigniter
中的数据
但出现错误“发生数据库错误”。我想我写错了代码
t.USER = $this->session->userdata('user_id');
代码如下:
function get_rekomen()
{
$query = $this->db->query("SELECT
c.*,
t.produk_id,
t.id_transdet,
t.kategori_id,
t.total_qty,
t.USER
FROM
transaksi_detail AS t
LEFT JOIN
(
SELECT
g.id_produk,
p.slug_produk,
p.foto,
p.foto_type,
p.harga_diskon,
p.diskon,
p.harga_normal,
p.judul_produk,
g.kat_id,
k.judul_kategori
FROM
(
SELECT
MAX(m.id_produk) AS id_produk,
m.kat_id
FROM
produk AS m
GROUP BY
m.kat_id
)
AS g
INNER JOIN
produk AS p
ON p.id_produk = g.id_produk
LEFT JOIN
kategori AS k
ON k.id_kategori = g.kat_id
)
AS c
ON c.kat_id = t.kategori_id
WHERE
t.USER = $ this -> session -> userdata('user_id');
<<// i think here's the problem
ORDER BY
total_qty DESC limit 1")->result();
return $query;
}
问题是什么是正确的代码?
答案 0 :(得分:0)
这是一个解决方案,但首先请确保已在控制器上加载$this->load->library('session');
,或者只需添加autoload.php
。
此外,您还必须向模型中添加参数,该参数将在会话中处理user_id
,因此例如,您的模型如下所示:
function get_rekomen($user_id) { ...your query here... }
然后使用user_id的条件将是
t.USER = $user_id;
因此,在您的控制器上,您可以像这样调用模型:
$user_id = $this->session->userdata('user_id');
$this->ModelClass->get_rekomen($user_id);
其他:在登录阶段,如果用户成功登录,则必须在会话上设置用户数据,例如$this->session->set_userdata('user_data', $session_data);
希望这会有所帮助!
答案 1 :(得分:0)
也许您可以使用它。您需要先定义$ user_id,然后再将其添加到 WHERE 语句...
$user_id = $this->session->userdata('user_id');
$sql = "SELECT c.*, t.produk_id, t.id_transdet, t.kategori_id, t.total_qty, t.USER
FROM transaksi_detail AS t
LEFT JOIN(SELECT g.id_produk, p.slug_produk, p.foto, p.foto_type, p.harga_diskon, p.diskon, p.harga_normal, p.judul_produk, g.kat_id, k.judul_kategori
FROM(SELECT MAX(m.id_produk) AS id_produk, m.kat_id
FROM produk AS m
GROUP BY m.kat_id)
AS g
INNER JOIN produk AS p ON p.id_produk = g.id_produk
LEFT JOIN kategori AS k ON k.id_kategori = g.kat_id)
AS c ON c.kat_id = t.kategori_id
WHERE t.USER = '$user_id'
ORDER BY total_qty DESC limit 1";
$query = $this->db->query($sql);
return $query->result();
希望这可以帮助您...