my query-function


it’s a little bit to much secific to my needs (e.g. i alrys use id as the auto-increment-id so it’s just chechig for that field and not for the real autoincid; and i reqests to have some $GLOBALS[‘MySQL…’] set), but still usefull.
// performs a mysql_query
// on first call it connects to the db
// returns info dependent on the statement:
// insert: auto increment id
// update: affected rows (thats how it may be used inside a if statement)
// select: 2 dimensional array with all rows
// if there is a field called id, it will be used as first array-key.
// (the order defined by ‘ORDER BY’ is not affected)
// second-level array-key is the fieldname (mysql_fetch_array)
// if no rows have been selected it returns an empty array
// (converts to bool false inside an if statement)
// else: mysql_info()
// if there was a MySQL error it will always return bool(false)
function query()
static $db;
or die(queryNoConnect());
$db=@mysql_selectdb($GLOBALS[‘MySQLdb’]) or die(queryNoDB());
$q=implode(’ ‘,$q);
if(1 and $e=mysql_error()) echo $e.’ '.$q;
if(!$r) return false;
if(isset($row[‘id’])) $byid=true;
if($byid) $a[$row[‘id’]]=$row;
else $a[]=$row;
return $a;
return @mysql_insert_id();
return @mysql_affected_rows();
return @mysql_info();

the $MySQL… vars have to be set (maybe inside an include file) like this: