gpt4 book ai didi

php - 在 PHP 中使用 api 时出现 SQL 语法错误

转载 作者:行者123 更新时间:2023-11-29 07:03:57 25 4
gpt4 key购买 nike

我从 guardian api 得到了响应,设法将其加载到一个变量中,我试图将内容放入相关的数据库表中,但出现以下错误:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://content.guardianapis.com/?format=json&show- fields=all&show-related=true&order-by=newest&show-most-viewed=true&api- key=srty8vfmpgjhjakk4k6edbjb");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
$response_api = curl_exec($curl);
curl_close($curl);

require_once 'Zend/Json.php';
$val = Zend_Json::decode($response_api);
foreach ($val['response']['mostViewed'] as $result) {
$title = $result['webTitle'];
$url = $result['webUrl'];
$body_text = $result['fields']['body'];
$title = utf8_decode($title);
$body_text = utf8_decode($body_text);


$sql="INSERT INTO news_data (title, content)
VALUES
('$title','$body_text')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}

mysql_close($con);

?>

我收到这个错误是因为文章中有奇怪的字符,我试图将其加载到某些变量中,然后加载到我的数据库中吗?

谢谢

JB

最佳答案

试试这个

$sql="INSERT INTO `news_data` (`title`, `content`)
VALUES
('".mysql_real_escape_string($title)."','".mysql_real_escape_string($body_text)."')";

关于php - 在 PHP 中使用 api 时出现 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8514116/

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