问题描述
我有以下 php
代码,它显示1列非常有用,但是我需要显示10列的值。
I have the following php
code and it's working great for showing 1 column, but I need it to show the values of 10 columns.
<select size="1" name="domeinnaam">
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '</option>';
}
?>
</select>
我尝试添加第二个 echo
,但是损坏了代码。我也试图
I have tried to add a 2nd echo
, but that corrupted the code. I also tried to
echo '<option>' . $row['domeinnaam1'] . $row['domeinnaam2'] . '</option>';
但没有工作。因为结果将显示如下:
domain1
domain1
domain1domain2
but that didnt work. because the result will then display as follows:domain1
domain1
domain1domain2
它应该是
domain1
domain1
domain1
domain2
domain1
domain1
domain1
domain2
什么工作?
推荐答案
假设您希望每个域名在select中显示为一个选项,并且db中的域名字段为domeinnaam1,你会做以下...
Assuming you want each domain name to appear as an option in the select and the domain name fields in your db are domeinnaam1, domeinnaam2, domeinnaam3, etc., you would do the following...
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
$domains = array();
while ($row = mysql_fetch_array($resultaat))
{
if (!empty($row['domeinnaam1'])) $domains[] = $row['domeinnaam1'];
if (!empty($row['domeinnaam2'])) $domains[] = $row['domeinnaam2'];
}
?>
<select size="1" name="domeinnaam">
<?php
foreach ($domains as $domain)
{
echo "<option>$domain</option>";
}
?>
</select>
您应该使用PDO而不是mysql_函数或ADODB库运行良好。 mysql_函数自PHP 5.5起已被弃用
You should use PDO instead of mysql_ functions or the ADODB library works well. mysql_ functions are deprecated as of PHP 5.5
参考,供PDO参考
这篇关于selectbox与mysql数据库有多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!