我正在尝试在存储在数据库中的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>';
如何使其工作或者您能告诉我如何在数据库中附加表格?
提前谢谢
编辑:用Jacobson的代码替换部分代码。仍然得到Attempt to assign property of non-object
答案 0 :(得分:0)
尝试替换此
$html = new simple_html_dom();
$html->load($result[0]);
用这个
$html = str_get_html($result[0]);
仍然,确保$ result [0]实际上是从数据库返回你的html表。