我是PHP的初学者,我想在Wordpress的页面下拉列表中显示来自数据库MySQL的数据。我用了这段代码:
<tr>
<td> Owner </td>
<td>
<select name="owner">
<?php
// $connection = mysql_connect(localhost, root, );
$connection = mysql_connect(localhost, root, );
$sql = mysqli_query($connection, "SELECT Nom FROM `herboristes`");
while ($row = $sql->fetch_assoc()){
?>
</select>
<option value="owner1"><?php echo $row['Nom']; ?></option>
</td>
</tr>
问题是我得到一个空的下拉列表,如下图所示:
请,任何建议。 谢谢!
答案 0 :(得分:0)
while ($row = $sql->fetch_assoc()){
?>
</select>
<option value="owner1"><?php echo $row['Nom']; ?></option>
</td>
</tr>
可能不见了?
<?php } ?>
答案 1 :(得分:0)
我真的不知道你想用什么? MySQL的?mysqli的?我不能知道.. 但你不应该使用mysql_
你可以看看 Why shouldn't I use mysql_* functions in PHP? ..所以我们会说你将使用mysqli
<tr>
<td> Owner </td>
<td>
<select name="owner">
<?php
// $connection = mysql_connect(localhost, root, );
$connection = mysqli_connect(localhost, root, ); //mysqli here
$sql = mysqli_query($connection, "SELECT Nom FROM `herboristes`");
while ($row = mysqli_fetch_assoc($sql)){ //mysqli here
echo '<option value="owner1">'. $row["Nom"] .'</option>'; //echo your options here
}
?>
</select>
</td>
</tr>
答案 2 :(得分:0)
<强>尝试强>
$connection = mysqli_connect("localhost", "root");
或强>
$connection = mysqli_connect("localhost", "root", "");
答案 3 :(得分:0)
不需要在wordpress页面中编写数据库连接 相反,我们可以使用
<tr>
<td> Owner </td>
<td>
<select name="owner">
<?php
global $wpdb;
$entries = $wpdb->get_results("SELECT Nom FROM herboristes");
foreach( $entries as $entry ) {
?>
<option value="<?php echo $entry->Nom; ?>"><?php echo $entry->Nom; ?> </option>
<?php } ?>
</select>