简体   繁体   English

如何在mysql中处理特殊字符

[英]How to handle special characters in mysql

I have a form that users enter data in and it gets entered into a mysql database. 我有一个表单,用户在其中输入数据,然后将其输入到mysql数据库中。 The issue is when they have entered a "%" sign or other special characters it causes problems when my website is trying to display the record. 问题是当他们输入“%”符号或其他特殊字符时,当我的网站尝试显示记录时会引起问题。 It actually causes nothing to be shown for that record when displaying results. 在显示结果时,它实际上不会导致该记录显示任何内容。 How do I fix this? 我该如何解决?

$query = "SELECT * FROM makerperk WHERE pid='$pid' LIMIT 1";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
while($row = mysqli_fetch_assoc($result)) {
    $makerid = $row['makerid'];
    $name = $row['name'];
    $title = $row['title'];
    $perkdescription = $row['perkdescription'];
    $image = $row['image'];
    $perktype = $row['perktype'];
    $restrictions = $row['restrictions'];
}

I think you should use PHP mysqli_real_escape_string 我认为您应该使用PHP mysqli_real_escape_string

/*Escape input variable:*/
$pid = mysqli_real_escape_string($connection, $pid);

/*Run query with escaped string:*/
$query = "SELECT * FROM makerperk WHERE pid='$pid' LIMIT 1";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
while($row = mysqli_fetch_assoc($result)) {
    $makerid = $row['makerid'];
    $name = $row['name'];
    $title = $row['title'];
    $perkdescription = $row['perkdescription'];
    $image = $row['image'];
    $perktype = $row['perktype'];
    $restrictions = $row['restrictions'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM