Case sensitive & searching for a part of the word

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kid_am
Forum Newbie
Posts: 7
Joined: Wed Nov 06, 2002 6:41 am

Case sensitive & searching for a part of the word

Post by kid_am »

Hey! I'm trying to make a search engine in my database. This one works, but is case sensitive and when you're searching for "bel", it wouln't find "belgium". Someone an idea how to create this? thx a lot!

Code: Select all

function country($woord1, $woord2) {
	echo "Zoeken naar" .$woord1. " en ".$woord2;
  	$link = mysql_connect("localhost", "root", "");
    if (!mysql_select_db("Project1", $link))
    	echo "Er is een probleem met de databankconnectie!";
    $country = mysql_query("SELECT * FROM Countries;");
    while ($row = mysql_fetch_assoc($country)) {
      foreach ($row as $col) {
        if($col == $woord1 || $col == $woord2){
          $Resultaat = X; 
          echo "<table width='90%' border='0' cellpadding='0' cellspacing='2'>";
          echo "<tr>"; 
          echo "<td class='header' width='16%'>Naam:</td>";
          echo "<td class='header' width='12%'>Populatie:</td>";
          echo "<td class='header' width='20%'>Leider:</td>";
          echo "<td class='header' width='20%'>Hoofdstad:</td>";
          echo "<td class='header' width='8%'>Aantal Kranten:</td>";
          echo "<td class='header' width='8%'>Opp. (km²):</td>";
          echo "<td class='header' width='26%'>Taal:</td>";
          echo "</tr>";
          echo "<tr>"; 
          echo "<td class='content'>".$row&#1111;"naam"]."</td>";
          echo "<td class='content'>".$row&#1111;"populatie"]."</td>";
          echo "<td class='content'>".$row&#1111;"leider"]."</td>";
          echo "<td class='content'>".$row&#1111;"hoofdstad"]."</td>";
          echo "<td class='content'>".$row&#1111;"kranten"]."</td>";
          echo "<td class='content'>".$row&#1111;"oppervlakte"]."</td>";
          echo "<td class='content'>".$row&#1111;"taal"]."</td>";
          echo "</tr>";
          echo "</table>";
        &#125;
      &#125;
    &#125;
    if (!($Resultaat))&#123;
      echo "<br><b>".$woord1."</b> werd niet in de databank gevonden!";
    &#125;

    mysql_close($link);
   &#125;
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

Check out:

http://www.php.net/manual/en/function.strncasecmp.php

and

http://www.php.net/manual/en/function.strlen.php

Check the length of what was entered into the search form and then use strncasecomp to search for the search term by it's length, this will make it case insensitive and return any values that have the same letters...so aus would return Austria and Australia...hop this helps...there is probably an easier way to do this but damned if I know it off hand...
kid_am
Forum Newbie
Posts: 7
Joined: Wed Nov 06, 2002 6:41 am

Post by kid_am »

great! thx a lot!
Post Reply