简体   繁体   English

无法获取Select2从我的MySQL数据库获取数据

[英]Unable to get Select2 to get data from my mysql database

I've been trying a few different methods of getting Select2 to use information from my MySQL database. 我一直在尝试几种不同的方法来让Select2使用MySQL数据库中的信息。 Currently, it seems something has been lost in translation and it's just showing the literal name of the array(".$title") . 目前,翻译似乎已经丢失了一些东西,它只是显示array(".$title")的字面名称。 The database is called vettigevrijdag and the table is called article . 该数据库称为vettigevrijdag ,表称为article

My PHP snippet that is to be included in the navbar: 我的PHP代码段将包含在导航栏中:

<head>
    <link href="select2.css" rel="stylesheet"/>
    <script src="select2.js"></script>
    <script>
      $(document).ready(function() {
      $('.js-example-basic-single').select2();
      });
    </script>
    <select class="js-example-basic-single">
    <?php
      include('db.php');
      $stmt=$db_con->prepare("SELECT * FROM article");
      $stmt->execute();
      if($stmt->rowCount() > 0){
        while($row=$stmt->fetch(PDO::FETCH_ASSOC))
        {
          extract($row);
          $id = $row['if'];
          $title = $row['title'];
        echo '<option value="'.$id.'">'.$title.'</option>';
        }
      }
    ?>
    </select>
</head>

My db.php file: 我的db.php文件:

<?php

$dbhost = "localhost"; /* Host name */
$dbname = "vettigevrijdag"; /* Database name */
$dbuser = "localhost"; /* User */
$dbpassword = "123456"; /* Password */


try {
    $db_con = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpassword);
    $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $ex) {
    die($ex->getMessage());
    //throw $th;
}
?>
<select class="js-example-basic-single">
<?php
  include('db.php');
  $stmt=$db_con->query("SELECT * FROM article")->fetchall(PDO::FETCH_ASSOC));
  foreach($stmt as $key=>$row){

    echo "<option value=\"".$row['if']."\">".$row['title']."</option>";
  }

?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM