使用PHP的MySQL simpleHTMLDOM插入数据时调用一个成员函数查找()一个非对象(Ca

2019-07-31 10:37发布

我有一个解析从网页的某些元素和将它们存储在MySQL数据库的脚本。 直到我试图分裂的元素,并将所得数据存储到数据库一切正常。 任何想法可能是错误的? 下面是我解析的页面: http://www.ratemyprofessors.com/SelectTeacher.jsp?sid=953

这里是我的代码:

<?php
include('simple_html_dom.php'); 

//connect to db (code emitted)

prof_List("http://www.ratemyprofessors.com/SelectTeacher.jsp?sid=953");

function prof_name($url)
{
    // collect data
    echo $url;
    $data = new simple_html_dom();  
    $data->load_file($url);
    $profName = $data->find("//*[@id=profName]", 0);
    $profName = strip_tags($profName);
    echo "Full Name: " . $profName = trim($profName);
    list($first, $last) = explode("&nbsp;", $profName);
    echo "fname: " .  $first;
    echo "lname: " . $last;

    //call mysql function
    insert_row($profName, $first, $last);

}


function insert_row($profName, $first, $last)
{
    $sql1="INSERT INTO PROFESSOR(name, firstname, lastname) VALUES('$profName','$first', '$last')";
    $sql1=strip_tags($sql1);
    echo $sql1;
    mysql_query($sql1) or die(mysql_error());  
    echo "Data Inserted!"; 
}


function prof_List($mainURL)
{

    $list = new simple_html_dom();  
    $list->load_file($mainURL);
    $profLinks = $list->find("//*[@class=profName]/a");
    foreach($profLinks as $profLink)
    {
        $profU=$profLink->href;
        echo $profURL = "http://www.ratemyprofessors.com/" . $profU;
        prof_name($profURL);
    }

}

?>

这里是我的输出:

Connected to MySQL
Connected to Databasehttp://www.ratemyprofessors.com/SelectTeacher.jsp?the_dept=All&sid=953&orderby=TLName&toggel=truehttp://www.ratemyprofessors.com/SelectTeacher.jsp?the_dept=All&sid=953&orderby=TLName&toggel=trueFull Name: fname: lname: INSERT INTO PROFESSOR1(name, firstname, lastname) VALUES('','', '')Data Inserted!http://www.ratemyprofessors.com/ShowRatings.jsp?tid=861228http://www.ratemyprofessors.com/ShowRatings.jsp?tid=861228
Fatal error: Call to a member function find() on a non-object in /Users/user1/Sites/simple_html_dom.php on line 879

Answer 1:

我不认为你可以使用find()simple_html_dom()

试试这个:

$data = new simple_html_dom();  
$data->file_get_html($url);
$profName = $data->find("//*[@id=profName]", 0);

此链接提供了一个巨大的,基本的例子。



文章来源: Call to a member function find() on a non-object when inserting data using PHP mySQL simpleHTMLDOM