phpmysql-error-1046

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected error


my code and I take a error. I wonder how I solve problems

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


}








?>

Solution

  • This is a solution you need for your code. Please replace it:

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

    I hope it is useful, regards :)