What is the difference between $variable and %$variable%? - php

I have this piece of code here:
$query = mysql_query("SELECT * FROM example WHERE text LIKE '%$value%'");
Would it make a difference if I would use:
$query = mysql_query("SELECT * FROM example WHERE text LIKE '$value'");
If yes, what would it be? What would be the difference?

Its not a difference in PHP, its a wildcard in SQL. You can read more about it here. Essentially, %
Matches any number of characters, even zero characters

Yes this has nothing to do with php, its a sql thing, % means like a wildcard there could be 0 or more characters instead of it.
%abc% matches abc, aabca, aabc, abcd
%abc matches dabc, abc but not abcd or tabcd
abc% matches abcd, abc but not dabc, tabcd

The difference is that '%$value%' will seach for matches containing $value (for exemple if $value = 'foo' it could return 'foobar' or 'barfoobar'), '$value' only matches the exact value of $value.

In PHP there is no difference (although you might want to take a look at using PDO for your database queries), the '%' symbols affect the query executed in MySQL.
% acts as a wildcard so the first result will return anything that contains the term 'value' in its text attribute, whereas the second will return only records that match exactly the term 'value'

Related

REGEXP not working as expected, inside a PHP script

I have this issue with mysql when querying a DB inside PHP.
The PHP code is:
$Query = "SELECT COUNT(*) FROM theTable WHERE fieldValue REGEXP 'Dom-R[eéèêë]my'";
$DBR = mysql_query($Query,$Connection);
I am expecting this query to get things like, I mean find the number of those:
Dom-Remy
Dom-Rémy
Dom-Rèmy
...etc...
But I get nothing, I mean zero. What is wrong in the code? I have tried several variations, all equally not working.
This is subject of Unicode characters.
What happens is that e,é,è,ê,ë.. in your example is not a single letter but 2 because the tilde counts as a character as well. This brings lots of complexities and rules that needs to be followed in order to meet Unicode rules.
You could do something like: ([\x{0049}-\x{0130}]) to search letters with tildes but this expression may vary depending if you are going to use this expression on .net, java, javascript or php.
You could also check what code each character represents here:
http://www.fileformat.info/info/unicode/char/search.htm?q=%C4%B0&preview=entity
As per official website specification, MySQL regex is matched in byte-wise fashion
The REGEXP and RLIKE operators compare characters by their
byte values and accented characters may not compare as equal even if a
given collation treats them as equal.
If you can match any character in place of [eéèêë], this should be sufficient:
$Query = "SELECT COUNT(*) FROM theTable WHERE field REGEXP '^Dom-R.+?my$'";
If
the column's CHARACTER SET is utf8 or utf8mb4, and
your connection between client and mysql server is also either of those character set, and
you are not using COLLATION utf8_bin, then
'Dom-Remy' = 'Dom-Rémy' = ...
WHERE ... = ... and WHERE ... LIKE ... will abide by the above. REGEXP (RLIKE) cannot be used, for the reasons already discussed.
This shows what is equal (for = and LIKE.)
If you are simply searching a string for Dom-Remy, use
fieldValue LIKE '%Dom-Remy%`
and instead of regexp/rlike
If you have something more complex that needs REGEXP, then start a new question with the details.

sql returning 0 rows even when where clause mathches

I am trying to execute
SELECT * FROM `product_laptop` WHERE name = "Acer Sdfsdf"
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010 seconds.)
even though there is an entry with the name Acer  Sdfsdf`
and name is also defined as unique key.
Maybe you should try like to get the correct data:
SELECT * FROM `product_laptop` WHERE name LIKE "Acer%"
Would that suit your needs?
The = requires an exact match, using like with the % wildcard will give you the results you want. This won't match exact records though and will allow for all sorts of variations.
SELECT * FROM `product_laptop` WHERE name like "Acer%Sdfsdf"
With LIKE you can use the following two wildcard characters in the pattern:
% matches any number of characters, even zero characters.
_ matches exactly one character.
http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html
You can use:
$name = preg_replace('/\s+/', '%', trim($name));
to convert all whitespaces to wildcards.

In mysql, how can I select the numeric portion of a field?

I have a table containing free-form phone numbers (e.g. '123-456-7890', '(987) 654-3210', or '123.456.7890') and I'm searching them with a numeric string- e.g., '123456'.
Right now, I'm having to do a bunch of replace(replace(replace(.. ad nauseum functions to strip out errant non-numeric characters then use that 'LIKE' the search value to search.
Anyone know of a simpler way to select only certain characters from a field based on type, not position?
You can use regex in MySQL -
SELECT *
FROM `table`
WHERE `phone` REGEXP '^[12345]'
Here is a working example.
try using regexes. if you want to extarct numbers from a string you can use this:
$string = '123-456-7890';
preg_match_all('/\d+/', $string, $matches);
echo $matches; //will result in only digits
Try splitting up the number into three different values, i.e.
$first = '124';
$second= '456';
$third = '7890';
"WHERE (phone_number LIKE '%$first%' and LIKE '%$second%' and LIKE '%$third%')"
I'm not certain this is as effective as SQL's REGEXP, but it should work since every part is sectioned out. May not work if a number is like this in the db:1245667890 because you can find 124,456, and 7890, but the number does not match. This answer assumes there will never be a solid number.

Split a string into formatted letters for Where IN query

This is sort of simplistic but I couldnt find anything that works for this particular situation.
Im trying to find a result via a Mysql query where the item in the db is one letter of a string. For example, I have the string 'MYSQL' and I need to retrieve everything in the DB with an identifier of M or and identifier of Q.
So the db looks like this
name identifier
item1 M
item2 Q
item3 B
and I want a search for 'MYSQL' to return item1 and item2.
A LIKE doesnt work so I tried a WHERE IN but that doesnt work unless I format the text like 'M','Y','S'...etc
which isnt out of the question but I have a feeling there is a more eloquent way to do it. Thanks in advance.
You may use this string function
SELECT * FROM table_name WHERE LOCATE(identifier, "mysql")>0
Use REGEXP.
If you create a regular expression with your single character identifier, you can match it to the word you want. For example:
select * from TABLENAME
where 'MySQL' REGEXP concat('.*', identifier, '.*');
That will get you the rows with 'M' and 'Q' but not the row with 'B'.
You might be doing your LIKE backwards.
In Oracle flavored syntax, you'd use: WHERE 'MYSQL' LIKE UPPER('%' || identifier_col || '%') where || is string concatenation and % is the wildcard character.
Why can't you use string concatenation and the LIKE operator?
SELECT name FROM table_identifiers
WHERE UCASE('MYSQL') LIKE UCASE(CONCAT('%', identifier, '%'))

Mysql regexp prevent character being number

Can someone help me with this bug. I need the following code to NOT match if there is another number afterwards
$query = "SELECT * FROM mytable WHERE server_name REGEXP '(server ?" . $server_id . ")' ";
For example, if $server_id is 50, it currently matches server 500, 501 etc and I dont want it too, but it should be allowed to match 'server50' 'server50 100mbit' 'server50,100mbit' etc. the character afterwards needs to be anything other than another number and can even be nothing at all.
Stu
Matching a non-digit is [^0-9] in regular expressions.
$query = "SELECT * FROM mytable
WHERE server_name REGEXP 'server ?" . $server_id . "([^0-9]|\$)' ";
You will want to do a regex against a number. /^0-9/ and what you would then need to do is to if returns a match, you need to say "Hey now, thats a number, wrong!" whereas if it returns an empty set, it would be a match.
You can also do a regex against it NOT being a number, where as you put the NOT character inbetween the ^ and the [.
I forget NOT syntaqx off the top of my head at the moment, but in common lingo, it would look like
/^![0-9]/
the slashes are usually the sign of a regex.
with your stuff, you need to do something like /^serverid[0-9][0-9]/ which will compare the regex with SERVERID[][] so it will only read the first 2 characters. If there are MORE characers, you can boot it out, or things like that

Categories