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.
[php]
// 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;
if(!$db)
{
@mysql_connect(
$GLOBALS[‘MySQLhost’],
$GLOBALS[‘MySQLuser’],
$GLOBALS[‘MySQLpass’])
or die(queryNoConnect());
$db=@mysql_selectdb($GLOBALS[‘MySQLdb’]) or die(queryNoDB());
}
$q=func_get_args();
$q=implode(’ ‘,$q);
$r=@mysql_query($q);
if(1 and $e=mysql_error()) echo $e.’ '.$q;
if(!$r) return false;
if(strtolower(substr($q,0,6))==‘select’)
{
$a=array();
$byid=false;
if($row=@mysql_fetch_assoc($r))
{
if(isset($row[‘id’])) $byid=true;
do
{
if($byid) $a[$row[‘id’]]=$row;
else $a[]=$row;
}
while($row=@mysql_fetch_array($r));
}
return $a;
}
if(strtolower(substr($q,0,6))==‘insert’)
return @mysql_insert_id();
if(strtolower(substr($q,0,6))==‘update’)
return @mysql_affected_rows();
return @mysql_info();
}[/php]

the $MySQL… vars have to be set (maybe inside an include file) like this:
[php]$MySQLuser=“user”;
$MySQLpass=“pass”;
$MySQLhost=“localhost”;
$MySQLdb=“databasename”;
$MySQLtableprefix=“qpoker_”;
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service