Jump to content

PHP search from database


Readme

Recommended Posts

How to make this: RED BOX

IF I Search "Mobile" and choose 2 letters. Then result is

Mobile

LG

 

IF

5 letters then Nokia

Must use LEN or something?

 

206zdz6.jpg

<?php
    mysql_connect("localhost", "", "") or die("Error connecting to database: ".mysql_error());
    mysql_select_db("data") or die(mysql_error());
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="container">
<form  action="search.php" method="GET">
Search:
<input type="text" name="query" />
 <input type="submit" value="Search" />
</form>
<div>
<label>Search categories</label>
<select>
<option value="1">Search all</option>
<option value="2">2 letters</option>
<option value="4">3 letters</option>
<option value="5">4 letters</option>
<option value="6">5 letters</option>
<option value="7">6 letters</option>
</select>
</div>
<?php
    $query = $_GET['query'];
    $min_length = 2;
       
    if(strlen($query) >= $min_length){
         
        $query = htmlspecialchars($query);
        
         
        $query = mysql_real_escape_string($query);
        
         
        $raw_results = mysql_query("SELECT * FROM articles
            WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());
           
           if(mysql_num_rows($raw_results) > 0){
             
            while($results = mysql_fetch_array($raw_results)){
                       
                echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
                           }
             
        }
        else{
            echo "No results";
        }
         
    }
    else{
        echo "Minimum length is ".$min_length;
    }
?>
 </div>
</body>
</html>
Link to comment
Share on other sites

your <select> menu has two apparent problems -
 
1) you must give the select menu a name attribute so that the value will have a name when it is submitted.
 
2) your select menu is not inside your <form></form>, so it won't get submitted anyway.
 
your php code doesn't have anything in it to get, validate, and use the value from the length select menu, but once it does, you would probably want to use the mysql CHAR_LENGTH(str) function in your sql query to match only text field lengths that have the specific length that was selected - http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_char-length

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.