Rumah >pangkalan data >tutorial mysql >Bagaimana untuk Mengisi Kotak Jatuh Bawah dengan Keputusan Pertanyaan MySQL dalam PHP?

Bagaimana untuk Mengisi Kotak Jatuh Bawah dengan Keputusan Pertanyaan MySQL dalam PHP?

Patricia Arquette
Patricia Arquetteasal
2024-11-15 02:15:02305semak imbas

How to Populate Dropdown Boxes with MySQL Query Results in PHP?

Populating Dropdown Boxes with MySQL Query Results in PHP

In PHP, you can easily populate dropdown boxes with data retrieved from a MySQL database. While you may have attempted to do this before, it's essential to understand each line of code to ensure successful implementation.

To achieve this, first ensure that your MySQL credentials are correct, especially if you're using a test environment. Here's an example that demonstrates the process:

  1. Database Connection:
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
  1. Query Execution:
$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);
  1. Dropdown Box Creation:
echo "<select name='PcID'>";
  1. Populating Options:

Iterate through the query results to create

while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
  1. Dropdown Box Closure:
echo "</select>";

This code will generate a dropdown box with options corresponding to the PcID values retrieved from the MySQL PC table.

Atas ialah kandungan terperinci Bagaimana untuk Mengisi Kotak Jatuh Bawah dengan Keputusan Pertanyaan MySQL dalam PHP?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn