PHP-based simple collection data warehouse receiving program [continued], php collection warehouse receiving sequel
In the previous article, we have collected the list data on the news page. The next step is to read the URL to be collected from the database and capture the page.
Create a content table
However, you must note that you cannot use the incremental method of id collection URL, because IDs in the data table may be intermittent, such as id = 9, id = 11, when id = 10 is collected, the URL is blank, which may result in empty fields being collected.
One technique used here is the database query statement. When we collect the first piece of data, we can determine whether there is an id number greater than this id in the database. If so, read one, the query information already exists.
The Code is as follows:
<? 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); // obtain the corresponding url address $ content = file_get_contents ($ row ['url']); $ pattern = "/<dd class = \" dataWrap \ "> (. *) <\/dd>/iUs "; preg_match ($ pattern, $ content, $ info); // obtain the information to store info echo $ title = $ row [1]. "<br/>"; echo $ content = $ info [0]. "
In this way, the news content we want will be collected into the database. Next we only need to sort out some data styles.
PHP collection warehouse receiving Problems
Php has the $ nr = implode ('#', $ arr) method.
However, the above is composed of "content 1 # Content 2", without the last #, if necessary
$ Nr = implode ('#', $ arr ).'#'
The stupid method is to use
Foreach ($ arr as $ vl ){
$ Nr. = $ vl ."#";
}
References: $
PHP collection warehouse receiving Problems
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!