Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Forked from AngeloR/lemon_mysql.php
Last active October 10, 2019 18:29
Show Gist options
  • Save luckyshot/5098146 to your computer and use it in GitHub Desktop.
Save luckyshot/5098146 to your computer and use it in GitHub Desktop.
A tiny function to interact with a MySQL database, can be used as a standalone PHP function or inside Limonade-php framework. Returns data in a multi-dimensional associative array. 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 re…
<?php
/**
* A quick little function to interact with a MySQL database.
* Updated by Xavi Esteve to use mysqli, original by AngeloR (https://gist.github.com/AngeloR/919695)
*
* 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
*
* Usage:
* // First start the connection
* $c = mysqli_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS, MYSQL_DATABASE);
*
* // Now run your queries
* [update|insert|delete]
* if(db('UPDATE mytable SET myrow = 4 WHERE someotherrow = 3', $c) !== false) {
* // worked!
* }
*
* [select]
* $res = db('select * from mytable', $c);
* echo $res[0]['id'];
*/
function db ($sql, $c) {
$res = false;
$q = ($c === null)?@mysqli_query($sql):@mysqli_query($c,$sql);
if($q) {
if(strpos(strtolower($sql),'select') === 0) {
$res = array();
while($r = mysqli_fetch_assoc($q)) {
$res[] = $r;
}
} else {
$res = ($c === null)?mysqli_affected_rows():mysqli_affected_rows($c);
}
}
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment