Apostrophe LIKE search in MYSQL not working - mysql

In MYSQL database, the title row is this:
Interview: Gus O\'Connor win
I am trying to search
title LIKE '%O\'Connor%'
I tried everything and still came up empty, no results returned.
I have tried
'%O''Connor%'
and
\"%O'Connor%"\
and yes, i am protecting myself from sql injection with code above.

You may try like this:
title LIKE "%O\\'Connor%"

If your database entry actually contains that backslash, you'll need to use this:
title LIKE "%O\\'Connor%"

Not sure why you are setting up a SQL string like this given the new PDO frame work which does allow not this syntax but anyways, try
LIKE "%O\\'Connor%"

Related

Replacing some URLs with another

I have a MySQL 5.7.29 database on which a website is built. I need to construct a query to find all table rows containing lines such as
https://example.com/index.php?topic=7989.0
or similar and replace them with
https://example.com/my-redirect-page/?7989.0
The wildcard here is the ?topic=7989.0 as this can be something like ?topic=1234 or even ?topic=3456.0#anchor
I can display the rows they appear in (in PHPMyAdmin) using this (thanks to 'sticky bit' below) :
SELECT * FROM `abc_posts` WHERE `post_content` LIKE '%example.com\index.php?topic%'
My problem is that I then need to change just that URL when there is also text content around it.
Thanks in advance.
The question mark doesn't need to be escaped.
But 'https://example.com/index.php?topic=7989.0' isn't LIKE '%example.com\?topic%' as the question mark doesn't immediately follow the host name.
Try:
...
post_content LIKE '%example.com/index.php?topic%'
...
You could do something like thin to find them
SELECT 'https://example.com/index.php?topic=7989.0'
WHERE 'https://example.com/index.php?topic=7989.0' REGEXP 'example.com/index.php?topic=';
Which would find all rows. But for replacing it, you must also tell which database version youh have mysql 5.x have not many regex fucntions mariadb and mysql 8 have much more

Using IN and LIKE in a search and replace query

I have some fields in a MYSQL database with the following content:
eq":"fieldname1+fieldname2+fieldname3+fieldname4/4
The numbers are always different, so it could also be something like:
eq":"fieldname11+fieldname22+fieldname8/10
I would like to run a query to achieve the following:
eq":"((fieldname1+fieldname2+fieldname3+fieldname4)/4, 2)
I currently have the following query
UPDATE wp_wrdydtdbww_cp_calculated_fields_form_settings
SET form_structure = REPLACE(form_structure, '???', '???')
WHERE id IN (1,2,3);
The problem is, that there are a lot of additional strings, containing 'fieldname' or '/' as well, so I need to replace the exact structure.
Can somebody help me to modify it?
I tried something with the LIKE pattern (%), but can't get it to work as I always replace other parts of strings as well.
Thanks!

issue with duplicating record in mysql query

When I search any record by chapter number then it works.
But the problem is when I select chapter number 1 or 2 from drop-down and the search all records included in that chapter.
It displays all records included in 1,11,21,31...or 2,21,12,...like this.
I know I wrote 'like' there that's why it happens. But when i write " = " operator that I commented in my code that also didn't work for me.
What will be the perfect query to solve this problem?
My Code:
<?php
include("conn.php");
$name=$_POST['fname'];
$name2=$_POST['chapter'];
$sql="SELECT distinct * FROM $user WHERE question like '%".$name."%' and Chapter like '%".$name2."%'";
// $sql="SELECT * FROM $user WHERE question='$name' and Chapter='$name2'";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
?>
I would be interested to see what the type of 'Chapter' is in the returned query, and try to see why it is that the equality comparison doesn't work.
If the typing is straightforward (i.e. it really is just plain old strings), then I'd be looking for whitespace characters or something like that which is foiling the equality comparison.
Similarly, I'm wondering whether it's the equality on the 'Question' that is messing up your alternate query.
At a guess, try one of the following:
$sql="SELECT distinct * FROM $user WHERE question like '%".$name."%' and Chapter like '$name2'";
$sql="SELECT distinct * FROM $user WHERE question like '%".$name."%' and Chapter='$name2'";
Oh, and you should really do something about escaping those parameters properly to avoid any nasty SQL injection attacks.
The problem is this part of the first query:
Chapter like '%".$name2."%'
If = doesn't work, then I can think of two things. The first is that Chapter is really a list, probably a comma delimited list. The second is that there are extraneous characters in the database.
If Chapter is really a list, use find_in_set() instead:
find_in_set($name2, Chapter) > 0
You directly use $_POST variables in your SQL query which makes you vulnerable to SQL injection attacks. Please take a look at this page for ways around that: http://bobby-tables.com/php.html. You should use either mysql_real_escape_string or prepared statements (better). The best solution would probably be to use PDO.
Also if you want a better answer, please format your question so it can be easily read, include example inputs, outputs and database contents and make sure your code is properly indented.
All I can assume now is that your database field probably contains more than the data you want to match. Leading/tailing spaces or something?
The main problem in your query is in this section
like '%".$name."%'
Just remove % sign from your whole query where you have wrote and check your query may work
properly.

Replacing a formatted string in MySql

I'm trying to replace all instances of an old BB tag markup in a MySql database with a newer, slightly different one.
The old format is this...
[youtube:********]{Video ID}[/youtube:********]
Which I would like to replace with this...
[youtube:********]http://www.youtube.com/watch?v={Video ID}[/youtube:********]
Where the *'s are a random string of alpha-numeric characters. So simply REPLACE(feild, '[youtube:********]', '[youtube:********]http://www.youtube.com?watch?v= won't do unfortunately.
All the clumsy attempts I've made using REPLACE() and INSTR() have resulted in nasty things like [b]Bold Text[/b]http://www.youtube.com/watch?v=
Is there a way to do this kind of pattern replacement in MySql? Possibly with Regular Expressions?
Thank you.
Is this what you tried?
UPDATE table SET Field = REPLACE(Field,']{',']http://www.youtube.com/watch?v={')
This would depend if there isnt any other occurences of ']{'
EDIT: You may also want to try:
UPDATE table SET Field = LEFT(Field,#) + 'http://www.youtube.com/watch?v='+
RIGHT(Field,(Char_Length(Field)-#);
Just check the syntax with MYSQl docs. Char_LNEGTH() may need to be LENGTH() - im sure you get the idea

Query MySQL with unicode char code

I have been having trouble searching through a MySQL table, trying to find entries with the character (UTF-16 code 200E) in a particular column.
This particular code doesn't have a glyph, so it doesn't seem to work when I try to paste it into my search term. Is there a way to specify characters as their respective code point instead for a query?
Thanks,
-Ben
Not tested, but CHAR() could work for this:
CHAR(0x200E);
I can't set up a full test case right now, let us know whether it worked.