SQL查询到选项表单

时间:2015-10-08 13:26:48

标签: php sql-server-2014

我有一个PHP代码来从我的Microsoft SQL Server 2014获取信息,但它无法正常工作,它自动工作的页面很好,因为当我注释掉PHP代码时它会弹出,但是一旦PHP代码没有注释掉,它只是全白,所以我假设P​​HP代码有问题。我必须将查询结果输入到下拉菜单中。

我使用此代码:

$servername = "VCCSQL03";
$username = "forecast";
$password = "Telefon2";
$dbname = "Forecast";

$connectionInfo = array("Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect($serverName, $connectionInfo);

if(!$conn) {
    echo "Connection could not be established.<br />";
    die( print_r( sqlsrv_errors(), true));
}

// Check connection
$result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct");
if ($result->num_rows > 0) {
    // output data of each row
    while($row = sqlsrv_fetch_array($result)) {
        echo "<option value='".$row['Brand_ProductID']."' name='".$row['Brand_ProductName']."'</option>";
    }
} else {
    echo "";
}
sqlsrv_close();

1 个答案:

答案 0 :(得分:0)

首先,您没有打开和关闭的select代码,并且您的option代码缺少>来正确关闭它。尝试下面的修订版,假设在页面上正确建立了连接,那么这应该可行。

$connectionInfo = array( "Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect( $serverName, $connectionInfo);

         if(!$conn) {
          //// Check connection
             echo "Connection could not be established.<br />";
                   die( print_r( sqlsrv_errors(), true));
               }

     $result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct"); 
    if ($result->num_rows > 0) {

   // output data of each row
    echo "<select name='products'>";
           while($row = sqlsrv_fetch_array($result)) {
         echo "<option value='".$row['Brand_ProductID']."'>$row['Brand_ProductName']</option>";
} 
echo "</select>";

    } else {
echo ""; } sqlsrv_close(); ?>