Page 1 of 1

This select method sucks... suggestions?

Posted: Thu Nov 10, 2005 4:57 pm
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;
	}

Posted: Thu Nov 10, 2005 5:05 pm
by foobar
Just use regular sql queries. It's simpler, more readable and more comprehendable. ;)

Posted: Thu Nov 10, 2005 5:09 pm
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