0

我的代码,我犯了一个错误。我想知道我如何解决问题

 <?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>";
    }


}








?>
4

1 回答 1

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>";
      }
  }
?>

我希望它是有用的,问候:)

于 2016-03-31T22:27:27.940 回答