473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. 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...
Apr 10 '10 #1

✓ answered by Atli

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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbLink = pg_connect("...");
  3.  
  4. // Note the lack of quote marks around
  5. // the place-holder.
  6. $sql = 'SELECT stuff FROM table
  7.         WHERE field LIKE $1';
  8.  
  9. // The wild-card is added here, in the value
  10. // rather in the query itself.
  11. $values = array('test%');
  12.  
  13. $result = pg_query_params($dbLink, $sql, $values);
  14.  
  15. // etc...
  16. ?>

2 6442
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbLink = pg_connect("...");
  3.  
  4. // Note the lack of quote marks around
  5. // the place-holder.
  6. $sql = 'SELECT stuff FROM table
  7.         WHERE field LIKE $1';
  8.  
  9. // The wild-card is added here, in the value
  10. // rather in the query itself.
  11. $values = array('test%');
  12.  
  13. $result = pg_query_params($dbLink, $sql, $values);
  14.  
  15. // etc...
  16. ?>
Apr 10 '10 #2
Its Great...Thank You Atli...for providing good explanation. my problem solved now...
Apr 12 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Aaron | last post by:
I'm writing a program that has an auto spelling correction feature. it is kind of like google and ebay's spelling suggestion features (did you mean?: correctly spelled word) I need to write a...
6
by: David Shorthouse | last post by:
Hello folks, I have a problem with an update query on an asp not updating the table in an Access db. The code runs, I have no errors, but when I examine the table, nothing was updated. The query...
3
by: Bruce D | last post by:
Here's the breakdown: MySQL 4.0.12 Table: Assignment (indexes are created on proper fields) 419,234 records Table: Finders (indexes are created on proper fields) 5,345,657 records My user wants...
1
by: Ryan Lange | last post by:
To start off, I'm running Apache 2.0.54, PostgreSQL 8.0.3 and PHP 5.0.3 on Windows XP and yes, libpq.dll is my system32 directory. I'm trying to do some queries using pg_query_params(), but I keep...
5
by: xeqister | last post by:
Greetings all, We have a complicated statement in DB2 which takes long hour to complete and we have created most of the indexes. Does anybody knows how to tune the following statement to optimize...
11
by: Randy Weber | last post by:
I am getting unexpected results with a Like Query using ASP and an Access database. This query - SELECT PN, Qty From Inventory Where PN Like 'SSW-10%'; returns what I expect: PN ...
12
by: Joe Stanton | last post by:
Hello Group I have a query that works in Oracle and SQL Server, but fails in Microsoft Access. The query is: SELECT data fromTABLE1 WHERE data>='A&' AND data<'A''' Here is my sample data:
1
by: =?Utf-8?B?S3Jpc3RpbmUgdGhlIERyYWdvbkxhZHk=?= | last post by:
Can anyone advise re spam emails which come through with the word V***gra (where the * are varying letters) - how do I specify the word just as I've typed the word with the asterisks? I can't find...
0
by: shamaila | last post by:
wild card characters are used to match strings as select * from table where name LIKE '%brown%'; but doesnt supports matching of dates,number what will we do if we want approximate matching of...
7
by: W. eWatson | last post by:
Is it possible to do a search for a wild card string in another string. For example, I'd like to find "v*.dat" in a string called bingo. v must be matched against only the first character in bingo,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.