I need to update values in a table by removing their last char if they ends with a +
Example:
John+Doe and John+Doe+ should both become John+Doe.
What's the best way to achieve this?
UPDATE table
SET field = SUBSTRING(field, 1, CHAR_LENGTH(field) - 1)
WHERE field LIKE '%+'
If you are trying to display the field instead of update the table, then you can use a CASE statement:
select
case
when right(yourfield,1) = '+' then left(yourfield,length(yourfield)-1)
else yourfield end
from yourtable
SQL Fiddle Demo
you didn't explain exactly the situation.
but if you search for names in a text. I'll remove all the non chars (anything not a-z and A-Z) including spaces and then compare.
if you want just the last char, try the SUBSTRING_INDEX function.
if you are passing to the DB as a string, you can do this with str_replace
<?php
$str = "John+Doe+";
$str = str_replace("+"," ",$str);
echo $str;
?>
Related
I have variables gathered from another website with preg_match_all and echo the value. But the value is in $var1[0][n], n>0 form;I couldn't inserted the value into mysql.
Thanks for any help.
$path='#<div class="comp-cell-row-div vtable infoColumn" style="width: 25%;">(.*?)</div>#si';
$vr12=file_get_contents('https://www....');
preg_match_all($path,$vr12,$ad12);
echo $ad12[0][14];
$value = array( var_dump($ad12));
foreach($value as $val){mysqli_query($conn,"UPDATE `test` SET `param1`='$val' WHERE `id`='118'");
}
or mysqli_query($conn,"INSERT INTO `test` VALUES .....");
First of all, we need to manipulate data. Choose db as varchar to see saved tags with data. Pregmatc_all also gets tags, strings in varchar format.
To eleminate unwanted tags or signs; use trim and substr expressions on php.
In the example I trim before 17th character. This is a property of strpos
$data12=trim(substr($ad12[0][n], 17, strpos($ad12[0][n],'')),$extratags);
mysqli_query($conn,"UPDATE `table` SET `val12` = '$data12' WHERE `col1` = 'data1'; ");
I'm trying to get a row from the database but when using binding. I know that this doesn't work because the query automatically puts single quotes so it will be like this: select model, magazine, round('name', 2) etc. This doesn't work of course but how do I get rid of the single quotes?
$merkinformation = DB::select('select Model, Magazine, round(?, 2) as Rondetijd from rondetijden where Merk = ? order by ? limit 3;', [$track, $merk, $track]);
You can't use column nmaes like this.
You must concatinate the name of the column. But this is vulnerable to sql injection. So you must check if $track has a valid content
$merkinformation = DB::select('select Model, Magazine, round(`' . $track . '` , 2) as Rondetijd from rondetijden where Merk = ? order by ? limit 3;', [$merk, $track]);
there is ['] single quote and [`] punctuation mark. If start with single quote or double quote mysql will translate that as string where punctuation mark will be recognize as field name.
Are you sure that is a single quote ?
I have this query:
$sql = "
INSERT INTO table SET
name = '$name',
sku = '$number',
description = '$desc'
";
But the rows containing some special characters (in my case this ') are not inserted.. How I can solve?
Thanks in advance.
When you construct your query, you need to escape the data you are inserting.
You need to at least use addslashes() function in PHP, like this:
$sql = "INSERT INTO table SET name = '".addslashes($name)."', sku = '".addslashes($number)."', description = '".addslashes($desc)."'";
However more correct way is to use a different function than addslashes, which would properly handle all characters in the data, not only apostrophes.
I am using my custom 'escape' function like this:
function escape($text)
{
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $text);
}
So using this function, you would write:
$sql = "INSERT INTO table SET name = '".escape($name)."', sku = '".escape($number)."', description = '".escape($desc)."'";
You must use parameterised queries instead of manually appending those values. Currently if name, number or description would contain any sql it would get executed.
A lot more detailed answer is in How can I prevent SQL injection in PHP?
Read about escaping characters in mysql. I think it is done with \
I have a table that has some rogue tags that need replacing
The offending string ends <tr> and needs replacing with </table>
Not all record are affected, so I need to find these and then replace them
Our skills using Update Replace Where are limited as the characters are not unique within the string but their position is, ie the last 4 characters
Have tried using
UPDATE table
SET field
REPLACE (RIGHT(field,4),</table>)
but suspec this is over simplified (and also fails)
try this:
UPDATE table
SET field=concat(left(field,length(field) -4),'</table>')
I had a similar situation in which needed to replace '_' from end of the transaction number field, where there where more than one occurrences of _ in field. Example: 20161124_C_BGN_5570.77_ & 20161121_C_HRK_1502360000__
Solution:
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -1)
WHERE RIGHT(transaction, 1) = '_';
// in case of double underscore (__)
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -2) # WHERE id = xxx WHERE RIGHT(transaction, 2) = '__';
I want to remove something from my table 1) 32) 121) 1000)... the format is number + )
I tried this code.
UPDATE articles SET
title= REPLACE(title,'\d)', '' )
WHERE title regexp "\d)*"
Nothing happened in phpmyadmin, how to write correct? Thanks.
You can't: Mysql doesn't support regex-based replace.
See this SO question for a work-around.
Finally, I use some php to solve this problem with a quickly method.
for ($i=1; $i<=9999; $i++){
$my_regex = $i.')';
mysql_query("UPDATE articles SET title = REPLACE(title,'".$i."', '' ) where title like '%".$i."%'");
}
I have unique requirement where I need to replace inactive owner username. Where username contians INACITVE followed by village id. So I have used concat() funacion inside replace() function to replace dynamically.
update Owner set username = replace(username, concat('_INACTIVE_',village_id) ,'')
where village_id = 3363010;
As an alternative, depending on the size of the table, you could do a workaround with substring function.