473,326 Members | 2,048 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,326 software developers and data experts.

Query with "_" in where gives incorrect result

Claus Mygind
571 512MB
I am using
MySQL v 5.0
php v 5.3

When I query the database I get an incorrect result if the value I use in the where clause contains an underscore "_".

1. If i use the "like" option with the % percent wildcard all rows are returned that contain the same character up to the underscore.
Result from sample code below
IPPI0001
IPPL0001
IPP_0001


2. If I use the "=" option with the % percent wildcard, I get no rows returned.

3. The only way it works is if I use the "=" option with no %
Result from sample code below
IPP_0001

I would most like to know why paragraph 1 above returns too many rows?

Here is some stand alone code that will demonstrate the problem. Simply plug in your database connection and run code. Thanks in advance for any help.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  try  // Error trap entire applet.; 
  3. {
  4.     /*open connection to YOUR database*/
  5.     //**********SET UP YOUR DATABASE CONNECTION**************************
  6.     $mysqli = new mysqli("localhost", "my_user", "my_password", "myDbName");
  7.  
  8.  
  9.  
  10.     $mysqli->query('DROP TABLE IF EXISTS `myDbName`.`myTest1`');
  11.  
  12.     $mysqli->query("CREATE TABLE myTest1 (`myKey` VARCHAR(8) not null default' ', PRIMARY KEY(`myKey`) ) ENGINE=MyISAM");
  13.  
  14.     $mysqli->query("INSERT INTO myTest1 set myKey='IPPI0001'");
  15.     $mysqli->query("INSERT INTO myTest1 set myKey='IPPL0001'");
  16.     $mysqli->query("INSERT INTO myTest1 set myKey='IPP_0001'");
  17.  
  18.  /* QUERY 1 using IPP_0001*/
  19.     $result = $mysqli->query("select myKey from myTest1 where myKey like'IPP_0001%'");
  20.     echo "query 1 using 'like IPP_0001'  INCORRECT RESPONSE<br>";
  21.     while ($record = $result->fetch_assoc()) 
  22.     {
  23.         echo '&nbsp;&nbsp;&nbsp;'.$record['myKey']."<br>";
  24.     }
  25.     $result->free();
  26.     $result = $mysqli->query("select myKey from myTest1 where myKey like'IPPL0001%'");
  27.     echo "query 1 using 'like IPPL0001'<br>";
  28.     while ($record = $result->fetch_assoc()) 
  29.     {
  30.         echo '&nbsp;&nbsp;&nbsp;'.$record['myKey']."<br>";
  31.     }
  32.     $result->free();
  33.  /* QUERY 2 */
  34.     $result = $mysqli->query("select myKey from myTest1 where myKey ='IPP_0001%'");
  35.     echo "query 2 using '= IPP_0001%'<br>";
  36.     while ($record = $result->fetch_assoc()) 
  37.     {
  38.         echo '&nbsp;&nbsp;&nbsp;'.$record['myKey']."<br>";
  39.     }
  40.     $result->free();
  41.     $result = $mysqli->query("select myKey from myTest1 where myKey ='IPPL0001%'");
  42.     echo "query 2 using '= IPPL0001%'<br>";
  43.     while ($record = $result->fetch_assoc()) 
  44.     {
  45.         echo '&nbsp;&nbsp;&nbsp;'.$record['myKey']."<br>";
  46.     }
  47.     $result->free();
  48. /* QUERY 3 */
  49.     $result = $mysqli->query("select myKey from myTest1 where myKey ='IPP_0001'");
  50.     echo "query 3 using '= IPP_0001'<br>";
  51.     while ($record = $result->fetch_assoc()) 
  52.     {
  53.         echo '&nbsp;&nbsp;&nbsp;'.$record['myKey']."<br>";
  54.     }
  55.     $result->free();
  56.     $result = $mysqli->query("select myKey from myTest1 where myKey ='IPPL0001'");
  57.     echo "query 3 using '= IPPL0001'<br>";
  58.     while ($record = $result->fetch_assoc()) 
  59.     {
  60.         echo '&nbsp;&nbsp;&nbsp;'.$record['myKey']."<br>";
  61.     }
  62.     $result->free();
  63. }
  64. ?>
  65.  
Oct 20 '14 #1

✓ answered by Rabbit

The underscore is also a wildcard character. The percent sign matches any number of characters. The underscore matches any one character. Basically, '%_%' will match anything that is at least one character long. You can escape wildcard characters with a backwards slash.

1 2115
Rabbit
12,516 Expert Mod 8TB
The underscore is also a wildcard character. The percent sign matches any number of characters. The underscore matches any one character. Basically, '%_%' will match anything that is at least one character long. You can escape wildcard characters with a backwards slash.
Oct 20 '14 #2

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

Similar topics

2
by: swhite76 | last post by:
I have a section of code that adds some double values and gives an incorrect result. This occurs with data that isn't really waht I would call high precision. An example is the following code...
2
by: Ellen Manning | last post by:
Using A2K. In my table I have the field "Grant" which can have a value or be null. I have a query that counts the number of records and has a Where clause on the Grant field. The query won't...
4
by: islandfong | last post by:
Hi there, I am implementing a reporting database which manipulating a huge amount of data. I used a lot of join. Just wondering which one performs better between the two scenarios: 1....
3
by: =?Utf-8?B?RmFiaW8=?= | last post by:
Hi Looking at the following code ... double a = 100.1; double b = 0.1; double c = a + b; I would like to know why the value of c is 100.19999999999999 and not 100.2?
4
by: Pietro Cerutti | last post by:
Hi group, #include <stdio.h> #include <unistd.h> #include <time.h> int main(void) { time_t t1, t2; char *st1, *st2;
2
by: eros | last post by:
CREATE OR REPLACE FUNCTION public.f_customerlogininfo ( varchar, varchar, varchar ) RETURNS SETOF public.v_customerlogininfo AS' declare p_schm alias for $1; p_contact alias...
5
AdusumalliGopikumar
by: AdusumalliGopikumar | last post by:
can anybody write a query using where,group by ,and having and explain me
5
by: Chris Kennedy | last post by:
Hi all I'm running SQL Server 2005 Express Edition. One database One table called Sites Fields as follows: id (bigint, Identity, Primary Key) SiteName (varchar(50), allows nulls) Generation...
7
by: kkshansid | last post by:
VHANDICAP = rs("HANDICAP") strSQL = "SELECT MAX(AMOUNT) FROM TABLE WHERE VHANDI= " & VHANDICAP & "'" Debug.Print strSQL rs.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.