0

I am working on a search engine for a website I'm making for practice.

I'm a newbie to PHP and wonder how I could add multiple SELECT FROM WHEREs

Here's the code:

$query = "SELECT * FROM search WHERE site_keywords LIKE '%$search_value%'";

$run = mysql_query($query);

while ($row=mysql_fetch_array($run)){

    $title = $row['site_title'];
    $link = $row['site_link'];
    $des = $row['site_des'];

    echo "<font face=\"Segoe UI Light\"><font size='5'><a href='$link'>$title</a></font></br >";
    echo "<font color='green'>$link</font>";
    echo "<p>$des</p><hr>";
}

I want it to search in site_keywords, site_title, site_link and site_desc.

Right now it only searches in site_keywords. How could I make it search in all?

I've tried some things now, and I always gets the same error, so here's the error, and the code that the error says is wrong:

Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:.xampp\htdocs\Practice Websites\PW2\results.php on line 24

I updated the code.

This question is different from others because I've searched for it and I've seen MANY answers. None of them work for me so I asked this question and included my code.

"mysql_fetch_array() expects parameter 1 to be resource (or mysqli_result), boolean given"

That question is not like mine. I want to know how to add multiple SELECT WHERE FROMs not what's wrong with the error I get. I already know that.

sjagr
  • 15,254
  • 4
  • 38
  • 65
Tutorial Nom
  • 21
  • 2
  • 6
  • `WHERE field1 = 'value' OR field2 = 'value'`... but reading a few MySQL tutorials will help you – Mark Baker Nov 10 '14 at 18:52
  • You need to use `OR` `site_keywords like '%$search_value%' OR site_title '%$search_value%'....` Or even better to look for Mysql Full Text Search. – Abhik Chakraborty Nov 10 '14 at 18:52
  • Don't work. Can you give me the whole code for the line? – Tutorial Nom Nov 10 '14 at 18:59
  • You should better use mysqli, (not related to the question, just a suggestion) because mysql is deprecated. – Zerquix18 Nov 10 '14 at 19:13
  • Use `$run = mysql_query($query) or die(mysql_error());` so you can see what the actual SQL error is. Put a `var_dump($query)` before the `mysql_query` too so you can see what your resulting query is before it breaks in MySQL. – sjagr Nov 10 '14 at 19:36
  • The reason it's not going to get reopened is because you're not cooperating by reading the other question on how to actually _get_ the error and putting it back up in your question. @Claudix answered your initial problem but you haven't given proper feedback as to what errors you get and what query you've ended up using when attempting to use his solution. – sjagr Nov 10 '14 at 19:42
  • Thank you! Now i know why it didn't work! It was search_des, not desc. – Tutorial Nom Nov 11 '14 at 15:37

1 Answers1

0

The WHERE clause is followed by a condition expression (i.e. something that evaluates to true or false). When the condition is true, the row is returned.

In order to create complex conditions there are some logical operators that allow combining several simpler conditions, being OR, AND and NOT the more usual operators. The LIKE operator returns true or false depending on there's a match between the two compared patterns. If you want the select to return a row if it matches several conditions, use the OR operator to combine several LIKE. For example:

$query = "SELECT * 
          FROM search 
          WHERE site_keywords LIKE '%$search_value%' 
             OR site_title LIKE '%$search_value%'
             OR site_link LIKE '%$search_value%'
             OR site_desc LIKE '%$search_value%'
";
sjagr
  • 15,254
  • 4
  • 38
  • 65
Claudix
  • 5,023
  • 14
  • 27
  • Gives me an error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\.xampp\htdocs\Practice Websites\PW2\results.php on line 24 – Tutorial Nom Nov 10 '14 at 19:02
  • 1
    Thank you! Now i know why it didn't work! It was search_des, not desc. – Tutorial Nom Nov 11 '14 at 14:59