0

I'm trying to match users with a certain description in one table to a general search description in another table. I have a foreach that contains the general descriptions and created a query within the foreach hoping to pull out the user that best fits that general description.

Problem is I'm not getting any results when I echo out the username. I get results when I echo out result['title'].

if (empty($errors)){
  $results = search_results($keywords);
  $results_num = count($results);

  echo '<p><strong style ="font-size:145%;font-family:georgia;"> Titles: </strong></p> ';

  foreach ($results as $result){
    $query = mysql_query("SELECT username, title from clothing 
    WHERE description LIKE '%$results%'");
    while($row=mysql_fetch_array($query)){
    $username=$row['username'];                     
    $title=$row['title'];
    echo $username;
    }
    echo '<p> <strong>',$result['title'],'</strong> </p>';
  }

}

I would be very grateful to know why my user is not getting echoed out. Also any programming tips would be greatly appreciated for I am always learning and I want to always find ways to improve.

6
  • Why don't you see if that query returns anything. As in, run it against the database in an SQL tool. Commented Oct 25, 2012 at 13:23
  • What does $result return on foreach(...) ? Commented Oct 25, 2012 at 13:24
  • are you sure your query returns >= 1rows? Commented Oct 25, 2012 at 13:24
  • 1
    In your query, you mean to use the foreach variable $result, not $results as you have: LIKE '%$results%'" Commented Oct 25, 2012 at 13:25
  • $result returns titles like it's supposed to @Robert Commented Oct 25, 2012 at 13:35

3 Answers 3

1

You need to replace $results with $result in your query:

$query = mysql_query("SELECT username, title from clothing WHERE description LIKE '%$result%'");
Sign up to request clarification or add additional context in comments.

Comments

0

Try to set one check (with error message)if count equals to 0 .. And LIKE clause value must be result not results

if (empty($errors)){
                $results = search_results($keywords);
                $results_num = count($results);

                if($results_num==0) {
                    echo "NO RESULTS MESSAGE !!!"; 
                }
                else{

                    echo '<p><strong style ="font-size:145%;font-family:georgia;"> Titles: </strong></p> ';

                    foreach ($results as $result){
                      $query = mysql_query("SELECT username, title from clothing WHERE description LIKE '%$result%'");
                      while($row=mysql_fetch_array($query)){
                        $username=$row['username'];                     
                        $title=$row['title'];
                        echo $username;
                      }
                      echo '<p> <strong>',$result['title'],'</strong> </p>';
                    }
               }

           }

Comments

0
SELECT username, title from clothing WHERE description LIKE '%$results%'

$results,it seems to be an array.I think you have to use $result here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.