Zig*_*igu 2 php sql codeigniter
我有这个查询,我想在我的PHP应用程序后端运行.在理论上,工作表是一个数据库,可以跟踪我们拥有的所有工作表.购买是一个数据库,用于跟踪哪些用户可以访问哪个工作表.我想要运行的查询被赋予用户的id,我可以获得他们应该有权访问的所有表.在查询表格中:
select distinct s.wsid, s.name from sheets s, purchases p where
s.wsid = p.wsid AND p.uid = *value*;
Run Code Online (Sandbox Code Playgroud)
其中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
}
}
Run Code Online (Sandbox Code Playgroud)
选项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;
}
}
Run Code Online (Sandbox Code Playgroud)
所有CodeIgniter Active Record命令的源:
codeigniter.com/user_guide/database/active_record.html
从某种方式做某事是否存在某种性能或安全性差异?这样做第二种方式对我来说似乎更让人困惑......这有点复杂,因为我不确定如何在这种编码风格中进行参考消歧,因为购买和工作表都有一个uid字段,但它们意味着不同的东西(除了首先不熟悉SQL join命令之外.).购买中的Uid(用户ID)表示用户已购买该表,而表中的Uid表示哪个用户拥有该表.
TL,DR:基本上,我要问的是,我是否应该花时间研究如何选择2方式?
主要好处是:
顺便说一句,如果您在PHP 5环境中,该库支持方法链接:
if( is_numeric($uid) ){
return $this->db->select('wsid, name')
->distinct()
->from('purchases')
->join('sheets', 'sheets.wsid = purchases.wsid')
->where('uid ='.$uid)
->get();
// nb. didn't check your join() syntax, either :)
}
Run Code Online (Sandbox Code Playgroud)
可能偏离主题:CodeIgniter的Active Record更像是一个查询构建器,而不是Active Record的实现.如果你想知道.当被视为查询构建器时,它会更有意义;)FWIW,我喜欢CodeIgniter.我从感情中开玩笑.