Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2010
    Posts
    33

    Unanswered: Need help on how to search where like ?

    Hello everybody.

    I need a little help on situation below.





    Code:
    <?php
    
    $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2");
    
    $query = 'SELECT * FROM XMLTABLE(
        XMLNAMESPACES (DEFAULT \'http://posample.org\'),
        \'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'
        COLUMNS
        "CID" VARCHAR (50) PATH \'@Cid\',
        "NAME" VARCHAR (50) PATH \'name\',
        "PHONE" VARCHAR (50) PATH \'phone [ @type = "work"]\'
        ) AS T
        WHERE NAME = ?
        ';
    
    $stmt = db2_prepare($conn, $query);
    
    $name = 'Kathy Smith';
    
    if ($stmt) {
        db2_bind_param($stmt, 1, "name", DB2_PARAM_IN);
        db2_execute($stmt);
    
        while($row = db2_fetch_object($stmt)){
        printf("$row->CID     $row->NAME     $row->PHONE\n");
        }
    }
    db2_close($conn);
    
    ?>
    PHP: db2_execute - Manual

    I've succeed to do 'like' search if I change instead of '=' search.
    Code:
    $name = 'Kathy Smith'; ->  $name like \'%Smith%\'
    So that I still can get the result if I only insert Smith.

    But the problem is I tired to do this with some php form.
    And my code look like this
    Code:
    <?php
    
    $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2");
    
    $query = 'SELECT * FROM XMLTABLE(
        XMLNAMESPACES (DEFAULT \'http://posample.org\'),
        \'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'
        COLUMNS
        "CID" VARCHAR (50) PATH \'@Cid\',
        "NAME" VARCHAR (50) PATH \'name\',
        "PHONE" VARCHAR (50) PATH \'phone [ @type = "work"]\'
        ) AS T
        WHERE NAME = ?
        ';
    
    $stmt = db2_prepare($conn, $query);
    
    $name = $_POST["name"];
    
    if ($stmt) {
        db2_bind_param($stmt, 1, "name", DB2_PARAM_IN);
        db2_execute($stmt);
    
        while($row = db2_fetch_object($stmt)){
        printf("$row->CID     $row->NAME     $row->PHONE\n");
        }
    }
    db2_close($conn);
    
    ?>
    In this case, how can I do the 'like' query.
    Where should I put the \'%*%\'.

    Thanks in advanced.

  2. #2
    Join Date
    Jun 2012
    Location
    Denver, CO, USA
    Posts
    20
    change:

    WHERE NAME = ?

    to:

    WHERE NAME LIKE ?

    and then in the binded parameter, change it to '%Smith%'

    I think that should work.

    ____________________
    Juacala - Web Application Developer, Eliacom, Inc.
    MySQL GUI Tools for Online Applications: Eliacom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •