gpt4 book ai didi

php - 从 xml 变量中过滤掉撇号

转载 作者:行者123 更新时间:2023-11-30 00:22:33 25 4
gpt4 key购买 nike

嗨,我使用一个小的 xml 脚本来过滤数据库中的所有条目。我的问题是,一些 xml 字符串的名称中有撇号,我需要在 mysql 数据库中过滤这些撇号。但是当我运行脚本时,所有数据都在那里,除了带有撇号的数据。这是我的代码:

include 'new.php'; //include xml file
$haus = new SimpleXMLElement($xmlstr);

´
foreach ($haus->features as $features) {
foreach ($features->properties as $properties) {
$name = $properties->name;
$insert = $mysqli->query("INSERT INTO locations (name)
VALUES ('$name')");
echo $mysqli->affected_rows;
}
}

有没有办法用php获取数据库中的撇号?

最佳答案

使用准备好的语句,这意味着您不必担心引号。这也将保护您免受 SQL 注入(inject)。还会有轻微的性能提升,因为您可以创建一次准备好的语句,然后多次执行它,而不必一遍又一遍地将整个查询发送到 MySQL 引擎。

$count = 0; // if you want to check how many rows were inserted

if($stmt = $mysqli->prepare('INSERT INTO locations (name) VALUES (?)')){
foreach ($haus->features as $features) {
foreach ($features->properties as $properties) {
$name = $properties->name;

$stmt->bind_param('s', $name);
if($stmt->execute()){
$count++;
}
}
}
}

echo 'total inserted: ' . $count;

关于php - 从 xml 变量中过滤掉撇号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23111061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com