我有2个看起来像这样的数据库表
订阅表
member_id active
5 1
7 0
8 1
计划表
member_id disk
5 2000
7 5000
8 3000
我希望为当前登录的用户显示disk
,其中active = 1.因此,如果当前登录的用户是member_id 8而他的active = 1
我们将显示3000.我们得到当前记录的在用户中$this->EE->session->userdata('member_id')
我有以下SELECT语句,但它不起作用。它正在抛出500 internal server error
。你觉得它有什么问题吗?
$results = $this->EE->db->query("SELECT disk FROM exp_membrr_plans p JOIN exp_membrr_subscriptions s ON p.member_id = s.member_id WHERE p.member_id = $this->EE->session->userdata('member_id') AND s.active=1 LIMIT 1");
$results->row('p.disk'); // the problem seems to be here. If i comment out this line i get no error
答案 0 :(得分:1)
将其更改为以下内容:
$results = $this->EE->db->query("SELECT disk FROM exp_membrr_plans p JOIN exp_membrr_subscriptions s ON p.member_id = s.member_id WHERE p.member_id = " . $this->EE->session->userdata('member_id') . " AND s.active=1 LIMIT 1");
答案 1 :(得分:1)
PHP的解析器有点愚蠢而不是“贪婪”。你在一个字符串中嵌入了一个多级对象调用,在PHP中是一个禁忌:
... p.member_id = $this->EE->session->userdata('member_id') AND ...
PHP的解析器会将$this->EE
视为对象调用,后跟一些纯文本->session->userdata(..)
。
要强制将整个调用层次结构解析为一个单元,您必须用{}
包围它:
... p.member_id = {$this->EE->session->userdata('member_id')} AND ...