如何在mysql数据库的字段中替换字符0x1f

时间:2015-08-18 18:25:17

标签: php mysql

我有一个数据库,无论出于何种原因,字段包含字符0x1f。我希望从包含0x1f的表中的每一行中删除此字符。

我能找到的最接近甚至找到受影响的行(有效)是“SELECT * FROM TABLENAME WHERE FIELDNAME<> CONVERT(FIELDNAME USING ASCII)”,但我对此没有任何喜悦,无论是在phpmyadmin中用php。

非常感谢任何帮助。谢谢。

编辑:混淆的应用。 0x1f不在字段名称中,它在字段row / s中。

EDIT2: 作为旁注,在网站上我使用了“$ title = preg_replace('/ [^ [:print:] \ r \ n] /','',$ title);”用于数据导出以删除不需要的字符,但已请求清理数据库本身。

EDIT3:

到目前为止,使用notepad ++的复制和粘贴工作是什么,但是我没有看到%FS%%FS%。我使用notpad ++从导出中复制了FS。 (希望有意义)

$mysqli->set_charset("utf8");
$sql="select post_title from wp_posts where post_title like '%%'";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
    //print_r($row);
    echo $row['post_title']."<br/>";
}

4 个答案:

答案 0 :(得分:1)

实际上在MySQL中插入 0x1f 值时,如果该列为 INT数据类型,它将存储为 31 。以下查询将非常有用。

  

从TABLENAME中选择* FEREDNAME = 31

如果列为 VARCHAR CHAR 数据类型,则会插入 0x1f 值。以下查询将非常有用。

  

从TABLENAME中选择* FEREDNAME ='0x1f'

如果某个角色中包含'0x1f',您可以使用

  

从TABLENAME中选择*,其中FIELDNAME类似'%0x1f%';

答案 1 :(得分:0)

你需要这样的东西:

UPDATE myTable
SET myCol = REPLACE(myCol, '%1f', UNHEX('1f'))
WHERE myCol LIKE '%\%1f%';

答案 2 :(得分:0)

根据其他答案和评论,最终的脚本有效。请记住,“%%”实际上是%FS%“其中FS是使用notepad ++查看的导出复制的,因为此文本框中的字符编码隐藏了字符0x1f。

我仍然想知道如果不使用php在mysql中这是否可行,那么此时会将问题保留为无答案。感谢大家的帮助。

function connect($host, $user, $pass, $db)
{
    $mysqli = new mysqli($host, $user, $pass, $db);
    if($mysqli->connect_error) 
        die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
    return $mysqli; 
}
$mysqli=connect("localhost","user","password","dbname");
$mysqli->set_charset("utf8");
$sql="select id,post_title from wp_posts where post_title like '%%'";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
    $title = preg_replace( '/[^[:print:]\r\n]/', '',$row['post_title']);
    $newsql="update wge8b198b_posts set post_title=\"".$title."\" where id=".$row['id'];
    $stmt2 = $mysqli->prepare($newsql);
    $stmt2->execute();
}
mysqli_close($mysqli);

答案 3 :(得分:0)

这将从表“ wp_posts”列“ post_title”中删除不需要的字符“ 0x1F”。

UPDATE [wp_posts]
SET [post_title] = REPLACE([post_title], CHAR(0x1F), '')
WHERE [post_title] LIKE'%' + CHAR(0x1F) + '%'