Jump to content

SQL Wildcards


Branden Wagner

Recommended Posts

$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.
Link to comment
Share on other sites

[code]
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;
}

[/code]

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?
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.