Home > Backend Development > PHP Tutorial > Simple data collection and warehousing program based on PHP [continuation], php collection and warehousing sequel_PHP tutorial

Simple data collection and warehousing program based on PHP [continuation], php collection and warehousing sequel_PHP tutorial

WBOY
Release: 2016-07-13 10:21:43
Original
693 people have browsed it

Simple data collection and warehousing program based on PHP [continuation], php collection and warehousing sequel

In the previous article, we have collected the list data of the news information page. The next step is to read the URL that needs to be collected from the database and crawl the page

Create a new content table

However, one thing to note is that you can no longer use the incrementing method of collecting URLs, because there may be id discontinuities in the data table, such as id=9, id=11. When the id=10 is collected, Sometimes, the URL is blank, which may result in empty fields being collected.

One of the techniques used here is the query statement of the database. When we collect the first piece of data, we determine whether there is an ID number greater than this ID in the database. If so, read one and repeat the query information above. work.

The specific code is as follows:

<&#63;php
  
  include_once("conn.php");
  $id=(int)$_GET['id'];
  $sql="select * from list where id=$id";
  $result=mysql_query($sql);
  $row=mysql_fetch_array($result);//取得对应的url地址
  $content=file_get_contents($row['url']);
  $pattern="/<dd class=\"dataWrap\">(.*)<\/dd>/iUs";
  preg_match($pattern, $content,$info);//获取内容存放info
  echo $title=$row[1]."<br/>";
  echo $content=$info[0]."<hr/>";

  //插入数据库
  $add="insert into content(title,content) value('$title','$content')";
  mysql_query($add);

  $sql2="select * from list where id>$id order by id asc limit 1";
  $result2=mysql_query($sql2);
  $row2=mysql_fetch_array($result2);//取得对应的url地址
  if($row2['id']){
    echo "<script>window.location='content.php&#63;id=$row2[0]'</script>";
  }

&#63;>

Copy after login

In this way, the news content we want has been collected and stored in the database. Next, we only need to organize some styles of the data.

PHP collection and warehousing problem

There is $nr = implode('#',$arr) method in php, that's it
But the above composition is "Content 1# Content 2" without the last #, if necessary
That’s $nr = implode('#',$arr).'#'

The stupid way is to use
foreach( $arr as $vl){
$nr .=$vl."#";
}
Reference: $

PHP collection and warehousing problem

mysql_connect() //Connect to your database first
mysql_select_db() //Select your database
mysql_query("insert into your table (address, title) values ​​('$tmp[1][ $i]',$tmp[2][$i])");//OK, done!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/854355.htmlTechArticleA simple data collection and warehousing program based on PHP [continuation], the continuation of PHP collection and warehousing is in the previous article, We have collected the list data of the news information page, and the next step is to start from...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template