我搜索过该网站但无法找到答案。
我在脚本中得到的是一个空的下拉菜单,而不是MySQL表中的内容。我正在NetBeans 8.0.2中开发/测试代码并在Firefox 34.0.5中运行。这是我的代码(文件名是'domains.php'),它完全如图所示;我没有遗漏任何东西。
<body>
<form method="post" action="">
<select id="domain" name="domain">
<?php
// define connection variables
$DBServer = "localhost"; // server name or IP address
$DBUser = "xxxxxxxxx";
$DBPass = "xxxxxxxxx";
$DBName = "country";
$DBPort = "3306";
// create a connection to mysql
$conn = mysqli_connect ($DBServer, $DBUser, $DBPass, $DBName, $DBPort);
// check to see if a connection was made and, if yes, proceed
if (mysqli_connect_errno()) { // connection failed
echo "Database connection failed: " . mysqli_connect_error();
}
// the domain query to get all domain names
$domainQuery = "select domains from domains_subdomains_cop";
// run the query -- this works elsewhere to display the contents of a table
$resultD = mysqli_query($conn, $domainQuery) or die ("Query to get data from domain failed: "
. mysql_error());
// w/the exception of pulldown menu, the while loop works well to display records
// I've seen examples with MYSQLI_ASSOC and without it and I've tried both without success for
// pulldown menus. I use it because I haven't had any issues elsewhere in my program.
while ($row=mysql_fetch_array($resultD, MYSQLI_ASSOC)) {
$domainName=$row[DOMAINS]; // DOMAINS is the table attribute I'm trying to pull from
echo "<option>
$domainName // I accidentely put a ';' here once and that did show up in the pulldown
// menu.
</option>";
}
?>
</select>
</form>
</body>
答案 0 :(得分:0)
下面是一个面向对象的mysqli预备语句版本
<form method="post" action="">
<select id="domain" name="domain">
<?php
$db = new mysqli($DBServer, $DBUser, $DBPass, $DBName, $DBPort);
$stmt = $db->prepare("select `domains` from `domains_subdomains_cop`");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_object()){
echo "<option>".$row->domains."</option>";
}
?>
</select>
</form>