我正在尝试在数据库中存储的 html 表中添加行。我正在使用PHP Simple HTML DOM Parser使用 MySQL。
这是函数:
<?php
include('simple_html_dom.php');
function addRow() {
//Connect to database and set charset
$link = mysqli_connect("localhost", "root", "", "webapp");
mysqli_set_charset($link, "utf8");
//Connection check
if (mysqli_connect_errno($link)) { exit("Failed to connect to MySQL: " . mysqli_connect_error()); }
//Selects html from MySQL and fetch it in array
$postContent = mysqli_query($link, "SELECT post_content FROM wp_posts WHERE ID=1");
$result = mysqli_fetch_array($postContent);
//Creates new simple html dom object
//and loads html from $result as first value from array
$html = str_get_html($result[0]);
//Change inner html of <tbody id="CZ">
$html->find("tbody[id=CZ]", 0)->innertext = '<tr><td></td><td></td><td></td><td></td><td></td></tr>';
}
?>
这是我来自 MySQL 的 html:
<table border="1"><tbody id="CZ"></tbody</table>
如果我运行它,我会收到此错误 Attempt to assign property of non-object in /function.php on line 21
其中第 21 行是:
$html->find("tbody[id=CZ]", 0)->innertext = '<tr><td></td><td></td><td></td><td></td><td></td></tr>';
如何才能使其工作,或者您能告诉我任何建议吗?我应该如何在数据库中附加表?
提前谢谢
编辑:用雅各布森的代码替换了部分代码。仍在获取Attempt to assign property of non-object
最佳答案
尝试替换这个
$html = new simple_html_dom();
$html->load($result[0]);
有了这个
$html = str_get_html($result[0]);
不过,请确保 $result[0] 实际上从数据库返回您的 html 表。
关于php - 使用 PHP 附加 html 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303412/