php - 通过php将xml数据插入mysql数据库

标签 php mysql xml

我想使用 file_get_contents() 读取 xml 文件,然后将此文件插入到我的 mysql 数据库中,但我的代码有错误,请参阅下面的代码:

//details ommited
$address= $_GET['address'];
$xml = file_get_contents($address);
db_connect(); // my db connection function
$query = "INSERT INTO feeds SET name = '$name' , xml_data = '$xml' ";
$result = mysql_query($query);
if(!$result)
{
   echo mysql_error();
}
// end of my code

所以,当我将 , xml = '$xml' 添加到我的 sql $query 时,php 向我显示此错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's OFF-state current.]]></description>\n\t\t\t</' at line 1

最佳答案

一些建议

  • 使用 PDO 参数绑定(bind)
  • 转义您的 xml 输入

根据您当前的操作方式,$xml 可能有一个字符会提前结束您的 sql 语句。我不知道什么角色可能会导致此问题,但我的建议应该可以解决这个问题。

关于php - 通过php将xml数据插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20690886/

相关文章:

android - 为什么 `android:foreground` 属性不起作用?

MySQL - 带有嵌套分组的嵌套选择

mysql加入同一张表不重复

xml - 使用 incron 和 bash 脚本调用 REST API?

php - 如何上传带有缩略图的多张图像并在上传前选择其中一张

php - OpenSSL HMAC-SHA1 摘要与 Crypto 的不匹配

php - 如何在特定结果布局中组合具有不同行数的表

javascript - UUID/GUID 生成器 JavaScript

c# - MySql参数声明c#rest web服务

c# - yt:在YouTube供稿中找不到统计信息