<?php
/*
	sql.php

	SQL function library

	Copyright (C) 2001-2007 Jeff Teunissen <deek@quakeforge.net>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

	See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to:

	Free Software Foundation, Inc.
	59 Temple Place - Suite 330
	Boston, MA  02111-1307, USA.
*/

have ('sql');

function sqlReadQuery ($query)
{
	global $sqlError;

	$ret = null;
	$sqlError = null;	// always set SQL error condition

	if (sqlAvail && $conn = @mysqli_connect (sqlHost, sqlUser, sqlPass, sqlDB)) {
		if ($result = @mysqli_query ($conn, $query)) {
			if ($numRows = @mysqli_num_rows ($result)) {
				for ($i = 0; $i < $numRows; $i++) {
					$ret[$i] = @mysqli_fetch_assoc ($result);
				}
			} else {	// Successful query, no rows returned.
				$ret = true;
			}
			@mysqli_free_result ($result);
		} else {	// Query failed.
			$sqlError = mysqli_error ($conn);
			$ret = false;
		}
		mysqli_close ($conn);
	} else {	// Couldn't even connect.
		if (sqlAvail)
			$sqlError = mysqli_error ($conn);
	}
	return $ret;
}


function sqlWriteQuery ($query)
{
	$ret = null;

	global $sqlError;

	$sqlError = null;	// always set SQL error condition

	if (sqlAvail && defined ('sqlRWUser') && defined ('sqlRWPass')
		&& $conn = @mysqli_connect (sqlHost, sqlRWUser, sqlRWPass, sqlDB)) {
		if ($result = @mysqli_query ($conn, $query)) {	// Success
			$ret = @mysqli_affected_rows ($result);
			@mysqli_free_result ($result);
		} else {	// Query failed.
			$sqlError = @mysqli_error ($conn);
			$ret = false;
		}
	} else {	// Couldn't even connect.
		if (sqlAvail && defined ('sqlRWUser') && defined ('sqlRWPass'))
			$sqlError = @mysqli_error ($conn);
		else
			$sqlError = "Cannot write to database.";
	}
	@mysqli_close ($conn);
	return $ret;
}
?>
