如何在HTML中将此PHP代码作为下拉菜单?

时间:2016-08-25 21:51:16

标签: php html sql-server-2008

这就是我的php代码:

<html>

Client list:<br><br>

<?php
$serverName = "NEPTUNE\SQLEXPRESS";
$connectionOptions = array("Database"=>"tcd", "UID"=>"tcd", "PWD"=>"technician");
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn === false ) {
die( FormatErrors( sqlsrv_errors() ) );
}

$sql = "SELECT client_name FROM client";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
echo $row[0]."<br />";
}

sqlsrv_free_stmt( $stmt);
?>

</html>

这将返回成功的查询。如何使用HTML中的select标签从下拉菜单中选择一个查询选项?

1 个答案:

答案 0 :(得分:1)

如果提取没有任何问题,只需像往常一样将其与HTML select + option标签结合使用

<html>

Client list:<br><br>

<?php

$serverName = "NEPTUNE\SQLEXPRESS";
$connectionOptions = array("Database"=>"tcd", "UID"=>"tcd", "PWD"=>"technician");
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn === false ) {
die( FormatErrors( sqlsrv_errors() ) );
}

$sql = "SELECT client_name FROM client";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}
?>

<select name="whatever">
    <?php while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ): ?>
    <option value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
    <?php endwhile; ?>
</select>

<?php sqlsrv_free_stmt( $stmt); ?>

</html>

注意:请确保将其包装在表单中(使用常用的表单方法post / get,buttons等)。语义是你自己决定的。