【发布时间】:2011-11-22 22:09:33
【问题描述】:
我想在我的 PHP 应用程序后端运行这个查询。从概念上讲,工作表是一个数据库,可以跟踪我们拥有的所有工作表。 Purchases 是一个数据库,用于跟踪哪些用户可以访问哪些工作表。我要运行的查询给出了用户的 ID,我可以获得他们应该有权访问的所有工作表。查询形式:
select distinct s.wsid, s.name from sheets s, purchases p where
s.wsid = p.wsid AND p.uid = *value*;
其中 value 是应用程序输入的内容
在我看来,有两种方法可以让它在后端工作。
选项 1)
public function getPurchasedSheets($uid){
if( is_numeric($uid) ){ //check against injections
$query = "select distinct s.wsid, s.name from sheets s, purchases p
where s.wsid = p.wsid AND p.uid = ".$uid.";" ;
return $this->db->query($query);
} else {
return NULL; //or false not quite sure how typing works in PHP
}
}
选项 2)
public function getPurchasedSheets($uid){
if( is_numeric($uid) ){
$this->db->select('wsid, name');
$this->db->distinct();
$this->db->from('purchases');
//not sure which order the join works in...
$this->db->join('sheets', 'sheets.wsid = purchases.wsid');
$this->db->where('uid ='.$uid);
return $this->db->get();
} else {
return NULL;
}
}
所有 CodeIgniter Active Record 命令的来源:
codeigniter.com/user_guide/database/active_record.html
以一种或另一种方式做事是否存在某种性能或安全差异?第二种方式对我来说似乎更令人困惑......这有点复杂,因为我不确定如何在这种编码风格中进行参考消歧,因为购买和工作表都有一个 uid 字段,但它们意味着不同的东西(除了一开始对 SQL 连接命令不是很熟悉。)。购买中的 Uid(用户 id)表示用户购买了该工作表,而工作表中的 Uid 表示哪个用户拥有该工作表。
TL,DR:基本上,我问的是我是否有理由花时间研究如何以选项 2 的方式做事?
【问题讨论】:
标签: php sql codeigniter