使用php curl将刮取的数据插入MySQL [英] Inserting scraped data using php curl into MySQL

查看:167
本文介绍了使用php curl将刮取的数据插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个脚本工作了最近几天,似乎找不到一种方法来插入数据到MySQL。我是一个初学者,当谈到PHP / MYSQL,并且只写了一些简单的脚本。我能够回显刮掉的数据,没有得到任何错误消息,但是当我检查phpmyadmin查询不工作(结果没有输入到数据库)。

I have been working on this script for the last couple of days and cannot seem to find a way to insert the data into MySQL. I am a beginner when it comes to PHP/MYSQL and have only written a couple of simple scripts before. I am able to echo out the scraped data and get no error messages, but when I check phpmyadmin the query isn't working (the results aren't being input to the database).

这是我一直在工作的代码

Here is the code that I have been working on

require ("mysqli_connect.php"); 
include('../simple_html_dom.php');

ini_set('user_agent', 
  'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');

// get DOM from URL or file
$html = file_get_html('http://www.asos.com/Women/Jeans/Cat/pgecategory.aspx?cid=3630&via=lhn');
// find all images

foreach($html->find('#items') as $a)       
    echo $a->innertext .'<br>';

foreach($html->find('span.price') as $p)
    echo $p->innertext .',';    

$q = "INSERT INTO jeans (`image`, `price`) VALUES ('$a', '$p')";
$r = @mysqli_query ($dbc, $q) or die ("Update query failed : " . mysql_error());;  //Run the Query.


推荐答案

在您的示例代码中,$ a和$ p对象,请改为尝试:

In your sample code, $a and $p are objects, try this instead:

$a = '';
foreach($html->find('#items') as $item) {
    $a.= $item->innertext .'<br>';
}

$p = '';
foreach($html->find('span.price') as $price) {
    $p.= $price->innertext.',';    
}

接下来,从@mysqli_query中删除@,尽量不要使用,请尝试正确地捕获/处理错误。

Next, remove the @ from @mysqli_query, try not to use that, ever, try to catch/handle errors properly instead.

接下来,请花几分钟时间研究参数化查询和PDO,不接受未知输入(来自第三方

Next, please take a few minutes to research paramaterized queries and PDO, don't accept unknown input (from 3rd parties no less) and inject them right in to your sql:

$q = "INSERT INTO jeans (`image`, `price`) VALUES ('$a', '$p')";

ie:不要这样做^

ie: Don't do that ^

最后,你可能想要验证get的响应。

Finally, you probably want to validate the response from the get.

希望有帮助!

这篇关于使用php curl将刮取的数据插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆