doutang3077 2019-08-07 10:33
浏览 56

我的表有两个时间戳值,剩下的是字符串值。 我想用PHP在html中显示整个表

I want to display entire sql table without knowing column names of that table. This table contains emp_id,emp_name,joining date,resignation_date,emp_address,emp_phone.

$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn) {
    echo "Connection established";
    if (isset($_POST['submit'])) {
        $selected_table = $_POST['cycle'];
        $query = "SELECT * FROM ".$selected_table;
        $result = sqlsrv_query($conn, $query);
        if (!$result) {
            $message = 'ERROR:'.mysql_error();

            return $message;
        } else {
            $i = 0;
            echo "<html><body><table><tr>";
            echo "the row value is ".sqlsrv_num_fields($result);
            while ($i < sqlsrv_num_fields($result)) {
                $meta = sqlsrv_get_field($result, $i);
                echo "<td>".$meta['name']."</td>";
                $i = $i + 1;
            }
            echo "</tr>";
            $i = 0;
            while ($row = sqlsrv_fetch_array($result)) {
                echo "<tr>";
                echo "rows are ".count($row);
                $count = count($row);
                $y = 0;
                while ($y < $count) {

                    $c_row = current($row);

                    echo "<td>".$c_row."</td>";
                    next($row);
                    $y = $y + 1;
                }
                echo "</tr>";
                $i = $i + 1;
            }
            echo "</table></body></html>";
            sqlsrv_free_result($result);
        }

    }
    sqlsrv_close($conn);
}

Recoverable fatal error: Object of class DateTime could not be converted to string

  • 写回答

1条回答 默认 最新

  • dongzong2017 2019-08-07 10:52
    关注

    Notes about your error:

    One explanation here is, that by default PHP Driver for SQL Server returns smalldatetime, datetime, date, time, datetime2, and datetimeoffset types as PHP DateTime objects. So, in this case, you have two options:

    • to parse the date as string using PHP DateTime::format
    • to set the 'ReturnDatesAsStrings' option in the connection string to true. By default this option is false.

    In your case, if you ... want to display entire sql table without knowing column names of that table ..., you should use the second option:

    <?php
        // Connection
        $server = "server\instance";
        $cinfo = array(
            "ReturnDatesAsStrings" => true,
            "Database" => "database",
            "UID" => "username",
            "PWD" => "password"
        );
        $conn = sqlsrv_connect($server, $cinfo);
        if( $conn === false )
        {
            echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
            exit;
        }
    
        ....
    ?>
    

    PHP example:

    You have issues with your script:

    • the statement with ... Object of class DateTime could not be converted to string ... error is probably echo "<td>".$c_row."</td>";
    • mysql_error() is function from different PHP extension. Use sqlsrv_errors() instead
    • you do not need to call current() and next() PHP functions to get each item in an array. Use foreach($array as $key => $value) { ... } instead.
    • you need to call sqlsrv_field_metadata() function to get field information.

    You may try with the next example, that will generate your expected output:

    <?php
    # Connection
    $server    = 'server\instance';
    $database  = 'database';
    $uid       = 'username';
    $pwd       = 'password';
    $cinfo = array(
        "Database" => $database,
        "ReturnDatesAsStrings" => true,
        "UID" => $username,
        "PWD" => $password
    );
    $conn = sqlsrv_connect($server, $cinfo);
    if( $conn === false )
    {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    # SQL statement
    $tablename = '[YourTable]';
    $sql = "SELECT * FROM ".$tablename;
    $stmt = sqlsrv_prepare($conn, $sql);
    if( $stmt === false ) {
        echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    # Columns names
    echo '<table id="'.$tablename.'">';
    echo "<thead>";
    echo "<tr>";
    $metadata = sqlsrv_field_metadata($stmt);
    if ($metadata === false) {
        echo "Error (sqlsrv_field_metadata): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    foreach($metadata as $field) {
        echo "<td>".$field['Name']."</td>";
    }
    echo "</tr>";
    echo "</thead>";
    
    # Table rows
    echo "<tbody>";
    if (!sqlsrv_execute($stmt)) {
        echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        echo "<tr>"; 
        foreach($row as $value) {
            echo "<td>".$value."</td>";
        };
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
    
    # End
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么