LoginSignup
0
0

More than 3 years have passed since last update.

DBのデータを連想配列で取得し、そのデータをoptionタグに使う

Posted at

ポイントは
・$result->fetch_assoc()により、連想配列としてデータを取得
・optionタグを記述する箇所は、whileの中


// DB接続の記述は省略


 <?php
 $result = mysqli_query($con, 'SELECT * FROM table_name');
 ?>

<select name="name">
 <?php
 // $dataがある限りoptionタグを生成する  
 while ($data = $result->fetch_assoc()) {
 ?>
<option value="<?php print $data['id']; ?>">
 <?php print $data['name'].": ".$data['point']."P"; ?>
</option>
 <?php
 }
 ?>
</select>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0