我想使用 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/