views:

54

answers:

3

Hey all

I am using MySQL and the 'like' query in order to find pages. I need to be able to do things this way, but this means that these pages will be the same:

www.mysite.com/blog/hello-world

www.mysite.com/blog/hello-worl

They both work, and bring up the same page. How (possibly using .htaccess?) can I get around this problem? :\

Many thanks

EDIT:

PHP -

if(isset($_GET['title'])) {
                $blogtitle=mysql_real_escape_string($_GET['title']);
                $title_two = strtolower(str_replace("-", " ", $blogtitle));
                $title_two = strtolower(str_replace("?", "", $title_two));
                $title_two = mysql_real_escape_string($title_two);

                $postmatch = "select * from blog where title LIKE '%".$title_two."%' LIMIT 1;";
                $postquery = mysql_query($postmatch) or die ("Could not match data because ".mysql_error());
                $blogtitle=mysql_result($postquery,0,"title");

                $title="$blogtitle";
+1  A: 

Can you provide a reason why you need to do it this way?

Do you require "hello-world" and "hello-world123" to match, but not "hello-worl" ?

Otherwise, I cannot think of any reason you would want to use a LIKE but not want to allow "hello-worl" to match.

Graphain
Because in order to get more information from the database I only have the page title to search the database by. So I need to get the title of the page first and find which page it corresponds to... I'm using $_GET['title'], and from there I want to find the ID.
Tim
Okay, but if it's impossible to have the id present in the URL, why do you want 'hello-world' and 'hello-world123' to match but not 'hello-worl'. What makes them special? If it's the whole title then why use LIKE?
Graphain
+1  A: 

Why not replace this:

$postmatch = "select * from blog where title LIKE '%".$title_two."%' LIMIT 1;";

...with this?

$postmatch = "select * from blog where title LIKE '$title_two' LIMIT 1;";

That way you're still searching regardless of capitalization, but not matching regardless of surrounding characters.

Matt Huggins
Thanks for this, only problem is that some of the $title_two's will have a question mark or something at the end :\
Tim
... and then they wont match to the field in the database
Tim
A: 

Well you have the the URL : www.mysite.com/blog/hello-world WHERE hello world is the search string

I recommended you to store them into array like

$keyWrd = array();
$keyWrd = strtolower($_GET['keywd']); // MUST DO VALIDATION
$keyWrd = explode(" ",$keyWrd);
$TotalKeyVal = count($keyWrd);

if(!empty($keyWrd[0]))
    {
        $SrcQryStr = "SELECT id,title,body FROM tbl_name WHERE";
        for($i=0;$i<$TotalKeyVal;$i++)
            {
                $SrcQryStr .= " title LIKE '%";
                $SrcQryStr .= $keyWrd[$i];
                $SrcQryStr .= "%'";

                $SrcQryStr .= ' OR ';

                $SrcQryStr .= " body LIKE '%";
                $SrcQryStr .= $keyWrd[$i];
                $SrcQryStr .= "%'";

                if(($TotalKeyVal-1)!=$i) $SrcQryStr .= ' OR ';
            }
        $SrcQryStr .= " ORDER by created_date DESC";

Now you have the $SrcQryStr Query string You can make more condition on if(!empty($keyWrd[0]) && MORE HERE) In here you can use implode or multiple implode instead of for loop.

Now why %.$keywd.%

Example :

STRING : Welcome to Stack Overflow!

Searched Keyword : ack

it will show match found because ack found on Stack WHILE %.$keywd.%

it will not show match found because there is no word which starts with ack WHILE $keywd.%

it will not show match found because there is no exact word ack WHILE $keywd

HADI