我想知道如何将数据插入我从其他网站中提取的数据库表
我的数据库表结构是:
<?php
require('admin/inc/simple_html_dom.php');
require('admin/inc/db.php');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://health.hamariweb.com/rawalpindi/doctors",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
));
$file = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
$dom = new simple_html_dom();
$dom->load($file);
$doctorDivs = $dom->find("#infinite-grid-images", 0)->children();
$doctors = array();
foreach($doctorDivs as $div){
$doctor = array();
$image = $doctor["image"] = "http://health.hamariweb.com".$div->find('img', 0)->src;
$details = $div->find('table', 1)->find("tr");
$name = $doctor["name"] = trim($details[0]->plaintext);
$spec = $doctor["Spec"] = trim($details[1]->plaintext);
$qua = $doctor["etc"] = trim($details[2]->plaintext);
$doctors[] = $doctor;
while($doctors){
mysqli_query($con, "INSERT into doctors(name, spec, qualification, image) VALUES('$name', '$spec', '$qua', '$image')");
}
}
echo "<pre>";
var_dump($doctors);
?>
这是我的PHP代码,它从外部提取数据
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Waypoint Name.kml</name>
<StyleMap id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
<Style id="sn_ylw-pushpin">
<IconStyle>
<scale>1.1</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<ListStyle>
</ListStyle>
</Style>
<Style id="sh_ylw-pushpin">
<IconStyle>
<scale>1.3</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<ListStyle>
</ListStyle>
</Style>
<Placemark>
<name>Waypoint Name</name>
<description>Description</description>
<LookAt>
<longitude>-114.4379986989632</longitude>
<latitude>62.47084160066127</latitude>
<altitude>0</altitude>
<heading>9.194188445440662e-10</heading>
<tilt>0</tilt>
<range>1000.000042077875</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Point>
<gx:drawOrder>1</gx:drawOrder>
<coordinates>-114.4435573708376,62.46869255035281,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
答案 0 :(得分:1)
从你的循环中取出$ doctors []数组。我对你的剧本做了一些改动,但是它有效。
require('admin/inc/simple_html_dom.php');
//require('admin/inc/db.php');
$db = new mysqli('localhost', DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($db, "utf8");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://health.hamariweb.com/rawalpindi/doctors",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
));
$file = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
$dom = new simple_html_dom();
$dom->load($file);
$doctorDivs = $dom->find("#infinite-grid-images", 0)->children();
$doctors = array();
foreach($doctorDivs as $div){
$doctor = array();
$image = $doctor["image"] = "http://health.hamariweb.com".$div->find('img', 0)->src;
$details = $div->find('table', 1)->find("tr");
$name = $doctor["name"] = trim($details[0]->plaintext);
$spec = $doctor["Spec"] = trim($details[1]->plaintext);
$qua = $doctor["etc"] = trim($details[2]->plaintext);
$doctors[] = $doctor;
}
//insert database part.
$data = $doctors;
foreach ($data as $val) {
$sql = "INSERT IGNORE INTO doctors (id, name, spec, qualification, image) VALUES (NULL, '".$val["name"]."' , '" .$val["Spec"]. "' , '" .$val["etc"]. "' , '" .$val["image"]. "');";
mysqli_query($db,$sql);
}
mysqli_close($db);
希望它有所帮助。