【问题标题】:Catchable fatal error Object of class DOMDocument could not be converted to string可捕获的致命错误 DOMDocument 类的对象无法转换为字符串
【发布时间】:2016-09-22 11:53:29
【问题描述】:

我迷失了这个错误:

可捕获的致命错误 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

【问题讨论】:

    标签: php mysql domdocument


    【解决方案1】:

    您不能简单地将DOMDocument 的实例用作查询中的字符串。您必须先将其显式转换为 HTML 字符串:

    $html = $doc->saveHTML();
    $result = $connection->db_query("CALL finalaggregator.insert_html('$html')");
    

    【讨论】:

    • 它不显示原始错误,而是转到“错误!数据保存过程不成功”。我能知道为什么吗?
    • 使用 error_reporting() 知道错误是什么,它显示“22527”。我知道如何解决这个问题吗?
    • 22527返回的值error_reporting()是一个BitMask,代表报错级别。在这种情况下,它代表E_ALL &amp; ~E_DEPRECATED,因此除了E_DEPRECATEDE_STRICT(不包括在E_ALL 中)之外,所有错误都会显示。
    猜你喜欢
    • 2015-03-08
    • 2013-08-07
    • 2018-03-12
    • 2014-02-21
    • 1970-01-01
    • 2015-11-21
    • 2014-06-21
    相关资源
    最近更新 更多