如何处理mysql中的特殊字符

9udxz4iz  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(245)

我有一个表单,用户在其中输入数据并将其输入mysql数据库。问题是当他们输入了“%”符号或其他特殊字符时,当我的网站试图显示记录时,会导致问题。它实际上导致在显示结果时不显示该记录的任何内容。我该怎么解决这个问题?

$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'];
}
y1aodyip

y1aodyip1#

我认为你应该使用php mysqli\u real\u escape\u 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'];
}

相关问题