This select method sucks... suggestions?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

This select method sucks... suggestions?

Post by Luke »

I made this select method to go with my database interaction class... but it isn't very flexible. What could I do to make it more flexible?

Code: Select all

function Select($searchword, $searchfield, $returnfields, $table, $relevance='=', $limit=1, $order=''){

		$sql = "SELECT ";
		if(is_array($inputfields)){
			foreach($returnfields as $val){
				$sql .= "`" . $val . "`,";
			}
			$sql = substr($sql, 0, -1);
		}
		else{
			$sql .= $returnfields;
		}
		$sql .= " FROM `" . $table . "` WHERE `" . $searchfield . "` " . $relevance . " '" . $searchword . "'";
		if(!empty($order)){
			$sql .= " ORDER BY `" . $order . "`";
		}
		$sql .= " LIMIT " . $limit;
		if($results = mysql_query($sql)){
			while($row = mysql_fetch_assoc($results)){
				$return[] = $row;
			}
			return $return;
		}
		logerror("Could not execute query: " . $sql . "\n MySQL says: " . mysql_error());
		return false;
	}
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Just use regular sql queries. It's simpler, more readable and more comprehendable. ;)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Whaddya mean? pass sql queries to the method? I need this to be able to accept user input and dynamically create a query...
most people don't know how to write sql queries
Post Reply