query with wild card characters in pg_query_params()...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abhilash Etikala
    New Member
    • Jan 2010
    • 21

    query with wild card characters in pg_query_params()...

    Hi everybody,
    I am using PGSQL database and i want to write query using pg_query_params () which contains wild card character ('%')

    query:
    Code:
    pg_query_params("select distinct compno,comp_name,sub from compl where comp_name like '%$1%' or sub like '%$2%' or comp_details like '%$3%' or action like '%$4%'",$arr);
    I tried by above query but i am getting the query failed:
    Query failed: ERROR: bind message supplies 4 parameters, but prepared statement "" requires 0.

    any suggestions on passing query parameters when we use wild card characters..
    And please correct me if any wrong...
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    The problem is that when you use a parametrized query like that. the database server adds the quotes around the values automatically (if needed). You should not quote it manually, or the place-holder will be ignored, making the query a parameter short of what you provided, which results in the error you posted.

    If you need to use a wild-card in your query, you need to add it to the value itself, not the query. For example:
    [code=php]<?php
    $dbLink = pg_connect("... ");

    // Note the lack of quote marks around
    // the place-holder.
    $sql = 'SELECT stuff FROM table
    WHERE field LIKE $1';

    // The wild-card is added here, in the value
    // rather in the query itself.
    $values = array('test%');

    $result = pg_query_params ($dbLink, $sql, $values);

    // etc...
    ?>[/code]

    Comment

    • Abhilash Etikala
      New Member
      • Jan 2010
      • 21

      #3
      Its Great...Thank You Atli...for providing good explanation. my problem solved now...

      Comment

      Working...