我迷失了这个错误:
捕获致命错误类DOMDocument的对象无法转换为字符串
这是我的PHP代码:
<?php
require_once('includes/mysqlConnect.php');
require_once('includes/utility.php');
//calling utility
$utility = new Utility();
//Creating a connection
$connection= new mySQL();
$connection->connect();
$getContent= file_get_contents('http://www.example.com/');
//echo $getContent;
//create a new DOMDocument Object
$doc= new DOMDocument();
//load HTML into DOMDoc
libxml_use_internal_errors(true);
$doc->loadHTML($getContent);
$utility->removeElementsByTagName('script', $doc);
$utility->removeElementsByTagName('style', $doc);
$utility->removeElementsByTagName('link', $doc);
echo $doc->saveHTML();
//Insert HTMl to DB
try
{
$result=$connection->db_query("CALL finalaggregator.insert_html('$doc')");
if ($result==0){
echo "<span style='color:red;'>Error! Data Saving Processes Unsuccessful</span>";
}
else {
echo "<span style='color:green;'>Data Successfully Saved!</span>";
}
}
catch (Exception $e){
echo "<span color='color:red;'>Error in Storing Data! Please Check Store Procedure.</span>";
}
?>
但总是最终显示
DOM文档无法在第29行转换为字符串
我想将$ doc的值存储到数据库中。
当我尝试从Mysql调用存储过程时:
call finalaggregator.insert_html("<p>Testing123</p>");
工作正常。
请帮帮我。我是php的新手。
我的存储过程如下:
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_html`( IN HTML LONGTEXT)
BEGIN
INSERT INTO finalaggregator.site_html (html) VALUES(HTML);
END
答案 0 :(得分:0)
您不能简单地在查询中使用DOMDocument
的实例作为字符串。您必须先将其显式转换为HTML字符串:
$html = $doc->saveHTML();
$result = $connection->db_query("CALL finalaggregator.insert_html('$html')");