1 Replies - 650 Views - Last Post: 19 July 2013 - 03:02 AM Rate Topic: -----

#1 Yeronox   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

No results returned from DB

Posted 19 July 2013 - 02:24 AM

This code should get the $keywords typed by the user and search in the DB (column: tags) if something is found. Then return the results
Right now this code returns no results, with a $count of 0, every time I type something, even if there should be some results
	function search_results($keywords)
	{
		global $db;
		$keywords = htmlspecialchars(trim($keywords));
		$returned_results = array();
		$keywords = preg_split('/[\s]+/', $keywords);
		
		$total_keywords = count($keywords);

		foreach ($keywords as $key => $keyword) {
			$where = "";
			$where = "$where `tags` LIKE '$keyword' ";
			if ($key != ($total_keywords-1) ) {
				$where .= " AND ";
			}
		}

		try {
				$query_results = $db->prepare("SELECT LEFT(`tags`, 50) AS `tags` FROM `images` WHERE :whe" );
				$query_results->bindParam(':whe', $where, PDO::PARAM_STR);
				$query_results->execute();
				$results_num = ($query_results->fetchColumn() > 0) ? $query_results->fetchColumn() : 0  ;
				echo $results_num;
				if ($results_num === 0) {
					return false;
				} else {
					while ($results_row = $query_results->fetch(PDO::FETCH_ASSOC)) {
						$results_row['tags'];
					}
				}
				return $results_row['tags'];
			} catch (PDOException $e) { exit($e->getMessage()); }
		
	}


Is This A Good Question/Topic? 0
  • +

Replies To: No results returned from DB

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4288
  • View blog
  • Posts: 13,594
  • Joined: 08-June 10

Re: No results returned from DB

Posted 19 July 2013 - 03:02 AM

Quote

Right now this code returns no results

naturally. to SQL your query looks like:
SELECT LEFT(`tags`, 50) AS `tags` FROM `images` WHERE "`tags` LIKE '$keyword'"


i.e. anything you pass in through the placeholders is exclusively treated as Data, not as SQL (even if it were SQL).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1