Skip to content

Instantly share code, notes, and snippets.

@AngeloR
Created April 14, 2011 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AngeloR/919695 to your computer and use it in GitHub Desktop.
Save AngeloR/919695 to your computer and use it in GitHub Desktop.
Simple mysql wrapper for lemonade-php
<?php
/**
* A quick little function to interact with a MySQL database.
*
* When working with Limonade-php a full-fledged MySQL wrapper seems like
* overkill. This method instead accepts any mysql statement and if it works
* returns either the result or the number of rows affected. If neither worked,
* then it returns false
*
* @param string $sql the sql statement you want to execute
* @param resource $c mysql connect link identifier, if multi-connect
* otheriwse, you can leave it blank
* @return MIXED array the result set if the sql statement was a SELECT
* integer if the sql statement was INSERT|UPDATE|DELETE
* bool if anything went wrong with executing your statement
*
*
* [update|insert|delete]
* if(db('update mytable set myrow = 4 where someotherrow = 3') !== false) {
* // worked!
* }
*
* [select]
* $res = db('select * from mytable');
*/
function db($sql,$c = null) {
$res = false;
$q = ($c === null)?@mysql_query($sql):@mysql_query($sql,$c);
if($q) {
if(strpos(strtolower($sql),'select') === 0) {
$res = array();
while($r = mysql_fetch_assoc($q)) {
$res[] = $r;
}
}
else {
$res = ($c === null)?mysql_affected_rows():mysql_affected_rows($c);
}
}
return $res;
}
?>
@AngeloR
Copy link
Author

AngeloR commented Apr 14, 2011

I use this as my quick sql wrapper for a standard mysql db when working with limonade. Normally I have a class that I use that abstracts away a lot of this, but when working with limonade for some reason that just feels really clunky. Might have to submit this as a Lemon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment