0

我正在从其他网站获取数据,谁能告诉我如何将获取数据插入 mysql 数据库

下面的代码我用来获取结果

require_once('config.php');
    //Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}

$urls = $_POST["urls"];
require_once('simple_html_dom.php');
$useragent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';

foreach ($urls as $url) 
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent); 
    $str = curl_exec($curl);
    curl_close($curl);

    $html= str_get_html($str);
    foreach($html->find('span.price') as $e)
        echo  $e->innertext . '<br>';
}

//Create INSERT query
$qry = "INSERT INTO scrapdata(price) VALUES('$e')";
$result = @mysql_query($qry);

//Check whether the query was successful or not
if($result) {
    echo ("Added Success");
}else {
    echo ("Not Added");
}
4

1 回答 1

2

试试这个:

$qry = "INSERT INTO scrapdata(price) VALUES('$e->innertext')";

或将 替换innertext为所需的值。

于 2012-12-13T06:33:56.177 回答