我的代码我发错了。我想知道我是如何解决问题的
<?php
include_once 'simple_html_dom.php';
try {
$dsn = "mysql:host=localhost;db=test";
$username ="root";
$password = "";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND=> 'SET NAMES UTF8' ,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION );
$conn = new PDO($dsn, $username, $password, $options);
} catch (Exception $ex) {
echo "Hata kodu " . $ex->getMessage();
}
// markaları alalım
$markaurl ="https://www.xxxx/chip-tuning";
$markaurlhtml = file_get_html($markaurl);
foreach ($markaurlhtml->find('div[class="darkGrid mediumBordered"] ul[id="brandsList"] a') as $markalar0) {
/* //Buna hiç gerek yokmuş üstteki kod gayet güzel ve esnek oldu :D
foreach ($markalar0->find('ul[id="brandsList"] a') as $markalar0) {
echo $markalar->href . "<br>";
} */
// echo $markalar0->href . "<br>";
// $mrk = $markalar0->href;
try {
$deyim = $conn->prepare("INSERT INTO remap_marka (marka) VALUES (?)");
$deyim->bindParam(1, $mrk);
$deyim->execute();
} catch (Exception $ex) {
echo $ex->getMessage() . "<br>";
}
}
?>
答案 0 :(得分:0)
这是您的代码所需的解决方案。请替换它:
<?php
include_once ("simple_html_dom.php");
try {
$dsn = "mysql:host=localhost;dbname=test";
$username = "root";
$password = "";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
$conn = new PDO($dsn, $username, $password, $options);
} catch (PDOException $ex) {
echo "Hata kodu " . $ex->getMessage();
}
// markaları alalım
$markaurl ="https://www.xxxx/chip-tuning";
$markaurlhtml = file_get_html($markaurl);
foreach ($markaurlhtml->find('div[class="darkGrid mediumBordered"] ul[id="brandsList"] a') as $markalar0) {
/*
// Buna hiç gerek yokmuş üstteki kod gayet güzel ve esnek oldu :D
foreach ($markalar0->find('ul[id="brandsList"] a') as $markalar0) {
echo $markalar->href . "<br>";
}
// echo $markalar0->href . "<br>";
// $mrk = $markalar0->href;
*/
try {
$deyim = $conn->prepare("INSERT INTO remap_marka (marka) VALUES (?)");
$deyim->bindParam(1, $mrk);
$deyim->execute();
} catch (Exception $ex) {
echo $ex->getMessage() . "<br>";
}
}
?>
我希望它有用,问候:)