gpt4 book ai didi

php - 在数据库中保存 file_get_contents

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:28 25 4
gpt4 key购买 nike

我想在我的站点数据库中保存 10.000 个页面。

当我运行该文件时,每个页面都会出现此错误。

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 '_trackPageLoadTime']);(function() {var ga = document.createElement("script"); ga' at line 1

我认为是某些字符导致了错误。

保存数据库

<?php 
include "conexao.php";

for ($nr=1; $nr<=10000; $nr++){
$url = "http://www.site.com/u$nr";
$html = file_get_contents($url);

set_time_limit(120);

$tabela_bd = "paginas";

$sql = mysql_query("INSERT INTO $tabela_bd(html) VALUES('$html')");
if ($sql) {echo "Cadastrado com sucesso!!";
} else {
echo "Falha ao cadastrar.".mysql_error();
}

}

?>

@编辑解决了我的字符问题,但现在有些表保存时没有内容。

最佳答案

不要使用已弃用的 mysql_* 函数。请改用 mysqli_* 函数。

更好的是,使用支持使用占位符的抽象库,例如 PDO。这会根据需要自动应用转义。见:

http://php.net/manual/en/book.pdo.php

占位符的使用示例:

http://www.php.net/manual/en/pdo.prepare.php

在你的情况下,也许是这样的:

$stmt = $pdo->prepare("insert into $tabela_bd( html ) values ( :html )");
$stmt->bindValue('html', $html);
$stmt->execute();

关于php - 在数据库中保存 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664321/

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