Colin McCann Dot Com

Custom PHP and MySQL Application Functions

Posted on September 12, 2011 at 10:27 pm

These are some custom PHP/MySQL functions that I use to speed up development as well as make the code more accessible. I find myself doing the same thing over and over and over again, and this relieves a bit of that monotony. Passing a secondary argument of 'true' (boolean) will output any errors the function may encounter; otherwise, it will fail silently.

Example Usage:

$sql = "SELECT * FROM `articles` WHERE `id` = {$id} LIMIT 1";
$article = return_row($sql, true);

Returns the first value of the first defined field

function return_field($sql, $show_errors = false) {
	if (empty ($sql)) {
		if ($show_errors) {
			echo 'Error in function return_field():<br />No SQL passed!';
		}
		return false;
	}
	$result = mysql_query ($sql);
	if (mysql_num_rows ($result) == 0) {
		if ($show_errors) {
			echo 'Function return_field() generated an empty result set:<br />Query:<br />'.$sql;
		}
		return false;
	}
	$row = mysql_fetch_array ($result);
	$data = $row[0];
	return $data;
}

Return a single MySQL row as an associative array

function return_row($sql, $show_errors = false) {
	// Run the query
	$result = mysql_query($sql);
	// Was the query valid?
	if (!$result) {
		if ($show_errors) {
			echo 'Error in function return_row():<br />'.mysql_error().'<br />Query:<br />'.$sql;
		}
		return false;
	}
	// Did you find anything?
	if (mysql_num_rows($result) <= 0) {
		if ($show_errors) {
			echo 'Function return_row() generated an empty result set:<br />Query:<br />'.$sql;
		}
		return false;
	}
	// We only want a single row
	if (mysql_num_rows($result) > 1) {
		if ($show_errors) {
			echo 'Function return_row() returned multiple records:<br />Query:<br />'.$sql;
		}
		return false;
	}
	// Okay then, let's give this a shot.
	$row = mysql_fetch_assoc($result);
	if (mysql_error()) {
		if ($show_errors) {
			echo 'Error in function return_row():<br />'.mysql_error().'<br />Query:<br />'.$sql;
		}
		return false;
	} else {
		return $row;
	}
}

Return multiple MySQL rows as an associative array

function return_rows($sql, $show_errors = false) {
	// Placeholder
	$rows = array();
	// Run the query
	$result = mysql_query($sql);
	// Was the query valid?
	if (!$result) {
		if ($show_errors) {
			echo 'Error in function return_rows():<br />'.mysql_error().'<br />Query:<br />'.$sql;
		}
		return $rows;
	}
	// Did you find anything?
	if (mysql_num_rows($result) <= 0) {
		if ($show_errors) {
			echo 'Function return_rows() generated an empty result set:<br />Query:<br />'.$sql;
		}
		return $rows;
	}
	// Okay then, let's give this a shot.
	while ($row = mysql_fetch_assoc($result)) {
		$rows[] = $row;
	}
	return $rows;
}
Code
1 comment for this entry:
  1. Bheem

    Great Thanks

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...