dongyong3554 2015-10-08 13:26
浏览 61
已采纳

SQL查询到选项表单

I have a PHP code to get info from my Microsoft SQL server 2014, but it isnt working, the page it self works fine since it pops up as it should when i comment out the PHP code, but as soon as the PHP code isnt commented out, its just all white, so im assuming problem with the PHP code. I have to get the results from the query out into a drop down menu.

i use this code:

$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条回答 默认 最新

  • dongpiansui8755 2015-10-08 13:37
    关注

    First and foremost, you do not have an open and closed select tag, and your option tags was missing a > to close it properly. Try the below revision, assuming connection is established on the page properly then this should work.

    $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(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?