| Welcome to WebmasterWorld Guest from 22.214.171.124 |
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
|Become a Pro Member|
|How do you do a bitwise compare in select where?|
I'm trying to select from a table by doing a bitwise compare but it does not work. Here is the sql select statement i have in my php script...
$Mask = implode($aom);
$sql = "SELECT * FROM dbtable WHERE ('".$Mask."' & Code)";
Basically; SELECT * FROM dbtable WHERE ('10100000000000' & Code)
$Mask is a 14 character string of ones and zeros i.e. "10100000000000"
Code is also a 14 character string field in the table similar to $Mask.
I want to do a bitwise compare of $Mask & Code with the "And" operator to test if Bits that are set in both $a and $b are set or true. if so, then get the record.
Like i said, the way i have it does not work. Is it because you can't do a bitwise compare on two strings?
A little help would be appreciated.
I don't do a lot with bitwise operators, but I think your query needs to look like this:
$sql = "SELECT * FROM dbtable WHERE '".$Mask."' & Code = '".$Mask."'";
While your solution won't work, you reminded me that ".$Mask. & code" must compare to something. Not "= .$Mask." but "<> '00000000000000'" for the where to work properly.
So the sql statement, i think, should look like this...
$Mask = '10100000000000';
$sql = "SELECT * FROM dbtable WHERE '".$Mask."' & code <> '"."00000000000000"."'";
As long as the bitwise operation of ".$Mask. & code" do not result in all zeros, i'll get records.
BTW, how did you get the php code to display in color?
unfortunately, it does not work because is appears the the bitwise and/or operators only work on integers not strings like you can in php.
What happens if you take out the single quotes? MySQL should then treat those as integers.
If your `Code` field is a VARCHAR (or other string) use the Cast functions [dev.mysql.com] to get it to the right type.
I give it try asap.
All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved