我已经看过至少一些有关此问题的重复问题但由于某种原因我无法将数据显示在菜单中。下拉菜单只是空白。
该表是“Colleges2”。我想要获取的数据位于名为“Name”的列中。因此它基本上从Colleges2表中获取并显示名称列表。
mysql_connect('localhost', '', '') or die(mysqli_error()) ;
mysql_select_db('Colleges2');
$sql = "SELECT Name FROM Colleges2";
$result = mysql_query($sql);
?>
<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
$Name = $row['Name'];
echo "<option value='" .$row['Name']. "'>" .$row['Name']. "</option>";
} ?>
</select>
我可能没有使用最新的mysqli代码....请原谅我。我一直看到使用mysql或mysqli的帖子和教程。
更新:
<?php
$sql = "SELECT Name FROM Colleges2";
$conn = new mysqli("localhost", "", "") or die("Failure!") ;
$stmt=$conn->query($sql);
?>
<select name="Name" id="">
<?php
while ($row = $stmt->fetch_assoc() ) {
$Name = $row['Name'];
echo "<option value='" .$Name. "'>" .$Name. "</option>";
} ?>
</select>
第二次更新(这一项工作):
<?php
//$host = "localhost:3306";
//$db_name="univers1_test";
//$user = "univers1_admin";
//$pass = "B@ctad89";
//$conn = new mysqli($host, $user, $pass, $db_name) or die("DB Connection failed!!");
mysql_connect("localhost:3306", "", "") or die(mysql_error()) ;
mysql_select_db("univers1_test") or die(mysql_error()) ;
$sql = "SELECT Name FROM Colleges2";
$result = mysql_query($sql) or die(mysqli_error()) ;
//$stmt=$conn->query($sql);
?>
<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php $row['Name']; ?>"><?php echo $row['Name']; ?> </option>
<?php
} ?>
</select>
答案 0 :(得分:1)
以下是如何使用PDO或MySQLi
<强> PDO 强>
<?php
$host = "localhost";
$db_name="Colleges2";
$user = "";
$pass = "";
$sql = "SELECT Name FROM Colleges2";
$conn = new PDO("mysql:host=$host;dbname=$db_name",$user,$pass) or die("DB Connection failed!!");
$stmt = $conn->prepare($sql);
$stmt->execute();
?>
<select name="Name" id="">
<?php
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
$Name = $row['Name'];
echo "<option value='" .$Name. "'>" .$Name. "</option>";
} ?>
</select>
<强>库MySQLi 强>
<?php
$host = "localhost";
$db_name="Colleges2";
$user = "";
$pass = "";
$sql = "SELECT Name FROM Colleges2";
$conn = new mysqli($host, $user, $pass, $db_name) or die("DB Connection failed!!");
$stmt=$conn->query($sql);
?>
<select name="Name" id="">
<?php
while ( $row = $stmt->fetch_assoc() ) {
$Name = $row['Name'];
echo "<option value='" .$Name. "'>" .$Name. "</option>";
} ?>
</select>
答案 1 :(得分:0)
<?php
mysql_connect('localhost', '', '') or die(mysqli_error()) ;
mysql_select_db('Colleges2');
$sql = "SELECT Name FROM Colleges2";
$result = mysql_query($sql) or die(mysqli_error()) ;
?>
<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['Name']; ?>"> <?php echo $row['Name']; ?> </option>
<?php
} ?>
</select>