2 Replies - 532 Views - Last Post: 22 March 2015 - 08:57 AM Rate Topic: -----

#1 feebee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-March 15

No results found in my Search engine

Posted 22 March 2015 - 08:31 AM

Hi! I need some help with my search engine. Whenever I submit a search (Eric), "No results for $search were found" appears on the screen. I think the error lies somewhere at the end of my code after
if ($foundnum==0) 


I have manually added the values for "keywords" into the database to match with the following existing columns; username, name, city, interests.
If possible I would also like to replace keywords in the $query with the other columns from my database (username, name, city, interests). I'm just not sure how to do this.

Example of my database-table:
Id(Int11)Value:1 | Username (VARCHAR100) Value: Eric | Name(VARCHAR100)Value: Eric | City(VARCHAR100) Value: New York Interests(VARCHAR100)Value: Soccer | Keywords(TEXT100)Value: Eric New York Soccer

<center>
<h1>Search</h1>

<input type='text' size='90' name='search' placeholder='Search for username, city, interests...'></br></br>
<input type='submit' name='submit' value='>>' ></br></br></br>
</center>
</form>

<?php print ("$output");?>



<?php
 
$search = $_GET ['search'];
$output = '';
 

$connection = mysqli_connect ($host, $user, $password, $dbname) or die("Cannot connect to database"); 
 
$search_exploded = explode (" ", $search);
$query ="SELECT * FROM user WHERE $query"; 

foreach($search_exploded as $search_each)
{
$x=0;
if($x==0)

		
$query .="keywords LIKE '%$search_each%'";
else
$query .="OR keywords LIKE '%$search_each%'";
 
}
 						

$query = mysqli_query($connection, $query);
 
$foundnum = mysql_num_rows($query);
 
if ($foundnum==0)
echo "No results for $search were found.";

else
{
echo "$foundnum results found !";
 
while($runrows = mysql_fetch_assoc($query))
{


$username = $runrows ['username'];
$name = $runrows ['name'];
$city = $runrows ['city'];
$interests = $runrows ['interests'];

$output .= '<div> '.$username.' '.$name.' '.$city.' '.$interests.' </div>';


 
}

}


?>



Is This A Good Question/Topic? 0
  • +

Replies To: No results found in my Search engine

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: No results found in my Search engine

Posted 22 March 2015 - 08:57 AM

One, your query needs spaces to work regardless of the results found. This is what the engine will see:

"SELECT * FROM user WHERE $querykeywords LIKE '%$search_each%'OR keywords LIKE '%$search_each%'"
Assuming the loop goes through twice.

Next issue is you are mixing mysqli_ with mysql_, don't do that! Also, PREPARED STATEMENTS!

Your table design is not scalable. Each record should be a single value, not a combination. It will speed up the query and make things easier to find as the database grows.



Try running the query through PHPmyAdmin and see if it returns a value. That tells you if the query is structured right. Then you can move on to implementing it in code.

That will get you started.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: No results found in my Search engine

Posted 22 March 2015 - 08:57 AM

the problem is line #22, at that point $query does not exist, hence your query becomes SELECT * FROM user WHERE and never changes after that.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1