Skip to content

Instantly share code, notes, and snippets.

@Dessix
Created July 14, 2012 22:57
Show Gist options
  • Save Dessix/3113799 to your computer and use it in GitHub Desktop.
Save Dessix/3113799 to your computer and use it in GitHub Desktop.
Reduction of PHP/MySQL Boilerplate for DB interaction via PDO
//Before:
public static function getTeamUsers($Team){
if(!static::checkvar($Team,'id'))
{
return false;
}
$sql = static::conn();
$sel = $sql->prepare("SELECT TeamMembers.UserID AS User FROM TeamMembers WHERE TeamMembers.TeamID = ?");
if($sql->error)
{
trigger_error("Preparation Error: ".$sql->error, E_USER_ERROR);
}
$sel->bind_param('i',$Team);
if(!$sel->execute())
{
trigger_error("MySQL Query failed to execute: ".$sel->error, E_USER_ERROR);
}
$tmlst = array();
$sel->bind_result($usr);
while($sel->fetch())
{
$tmlst[] = $usr;
}
$sel->close();
return $tmlst;
}
//After
public static function getTeamUsers($Team){
if(!static::check($Team,'i'))
{
return false;
}
$ulst = static::getAll('SELECT TeamMembers.UserID AS UID FROM TeamMembers WHERE TeamMembers.TeamID = ?', array(1=>$Team));
$outlst = array();
foreach($ulst as $usr)
{
$outlst[] = $usr['UID'];
}
return $outlst;
}
//It is worth noting that the foreach loop in 'After' is not part of the DB interaction, rather a way to translate back to our
///deprecated method of reading DB data, so that the interface remains compatible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment