Jump to content


Photo

SQL Wildcards


  • Please log in to reply
4 replies to this topic

#1 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 12:52 AM

$keytype = "LastName";
$keyword = "Smith";

$result= mysql_query("SELECT * from Customers where $keytype like '%". $keyword ."%'");

if i use it like that it returns both entries as it should for both bob and john smith.
however if i search..
$keyword = "Smi";

or any other partial match it returns nothing.. what am i doing wrong? and what do i need to do SQL wildcards i thought % was a wildcard.

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 23 July 2006 - 01:02 AM

yes, there's nothing wrong, and yes, it should return the same result as with 'Smith' or 'smith' or 'smi'.

I think something else is wrong.
Life's too short for arguing.

#3 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 01:04 AM

function find_customer($keytype,$keyword)
{
        //Conect to DB
        mysql_connect($db_host, $db_user, $db_pass);
        mysql_select_db("$db_name") or die(mysql_error());

        switch($keyword)
        {
                case "CustomerID":
                case "PhoneNumber":
                        $result= mysql_query("SELECT * from Customers where $keytype='". $keyword ."'");
                        break;
                case "FirstName":
                case "LastName":
                case "CompanyName":
                        $result= mysql_query("SELECT * from Customers where $keytype like '%". $keyword ."%'");
                        break;
                default:
                        $result= mysql_query("SELECT * from Customers where $keytype='$keyword'");
                        break;
        }

        while($row = mysql_fetch_assoc($result))
        {
                $customer[] = array(
                        $row['CustomerID'], $row['FirstName'],$row['LastName']
                );
        }
        return $customer;
}


well theres my whole query function.. also i just ran the query from MySQL command line and your right it does work the way it should... but its not here(in php). suggestions?


#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 23 July 2006 - 01:08 AM

you should switch $keytype instead of $keyword:

change
        switch($keyword)
to
        switch($keytype)
Life's too short for arguing.

#5 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 01:10 AM

I HATE YOU ;D

lol... i knew there was something simple..


Thanks... that did it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users