dounao1856 2017-04-27 18:32
浏览 44
已采纳

PHP:MSSQL查询一页上的多个字段

I am new to PHP, and have a web form, that I am using PHP to get data from the database. What I have currently done, as I havent been able to find out another solution to do so (despite my searching - probably dont know the correct terms to look for), is individually executing a SQL Query for each input field on my form. As below:

            <div class="search-line">
                <div class="search-option">
                    <label>Asset Tag:<i title=""></i></label>

                    <?php

                    $asset_tag_sql = "SELECT HardwareAsset.HardwareAssetAssetTag FROM HardwareAsset WHERE HardwareAssetID = '".$_SESSION["HardwareAssetID"]."'";
                    $asset_tag = sqlsrv_query($database_connection, $asset_tag_sql);

                    ?>

                    <input type="text" id="AssetTag" disabled value="<?php while ($asset_tag_option = sqlsrv_fetch_object($asset_tag)){echo $asset_tag_option->HardwareAssetAssetTag;} ?>" />
                </div>
                <div class="search-option">
                    <label>Serial Number:<i title=""></i></label>

                    <?php

                    $serial_number_sql = "SELECT HardwareAsset.HardwareAssetSerialNumber FROM HardwareAsset WHERE HardwareAssetID = '".$_SESSION["HardwareAssetID"]."'";
                    $serial_number = sqlsrv_query($database_connection, $serial_number_sql);

                    ?>

                    <input type="text" id="SerialNumber" disabled value="<?php while ($serial_number_option = sqlsrv_fetch_object($serial_number)){echo $serial_number_option->HardwareAssetSerialNumber;} ?>" />
                </div>
            </div>

Is there anyway to have one PHP piece of code to do one SQL query and then use that to fetch and echo the value for both input fields, as opposed to the two above?

  • 写回答

2条回答 默认 最新

  • dtewnsdf47253 2017-04-27 18:42
    关注

    Try it like this?

    <?php
    $asset_sql = "SELECT HardwareAsset.HardwareAssetAssetTag,HardwareAsset.HardwareAssetSerialNumber FROM HardwareAsset WHERE HardwareAssetID = '".$_SESSION["HardwareAssetID"]."'"
    $asset_result = sqlsrv_query($database_connection, $asset_sql);
    $asset_data = sqlsrv_fetch_object($asset_result);
    ?>
    
    <div class="search-line">
        <div class="search-option">
            <label>Asset Tag:<i title=""></i></label>
            <input type="text" id="AssetTag" disabled value="<?php echo $asset_data->HardwareAssetAssetTag; ?>" />
        </div>
        <div class="search-option">
            <label>Serial Number:<i title=""></i></label>
            <input type="text" id="SerialNumber" disabled value="<?php echo $asset_data->HardwareAssetSerialNumber; ?>" />
        </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?