phpfind mysql怎么用,使用PHP mySQL simpleHTMLDOM插入数据时,在非对象上调用成员函数find()...

I have a script that parses certain elements from a webpage and stores them in a mysql db. Everything works fine until I try to split an element and store the resulting data into the db. Any ideas what might be wrong?

Here's the page I'm parsing: http://www.ratemyprofessors.com/SelectTeacher.jsp?sid=953

Here's my code:

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(" ", $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);

}

}

?>

Here's my output:

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

解决方案

I don't think you can use find() on simple_html_dom().

Try this:

$data = new simple_html_dom();

$data->file_get_html($url);

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

This link provides a great, basic example.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值