Jump to content

Archived

This topic is now archived and is closed to further replies.

Branden Wagner

SQL Wildcards

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.

Share this post


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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
you should switch $keytype instead of $keyword:

change
        switch($keyword)
to
        switch($keytype)

Share this post


Link to post
Share on other sites

×

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.