我的表有两个时间戳值和剩余的字符串值。 我想用PHP在HTML中显示整个表(My table h

2019-09-30 02:42发布

我想不知道表的列名,以显示整个SQL表。 此表包含EMP_ID,EMP_NAME,入厂日期,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);
}

可恢复致命错误:类的DateTime的对象无法转换为字符串

Answer 1:

关于你的错误的注意事项:

这里的一个解释是,对于SQL Server返回默认PHP驱动smalldatetimedatetimedatetimedatetime2 ,和datetimeoffset类型为PHP的DateTime对象。 所以,在这种情况下,你有两个选择:

  • 解析日期使用PHP字符串DateTime::format
  • 设置'ReturnDatesAsStrings'连接字符串中的选项true 。 默认情况下,这个选项是false

在你的情况,如果你想...不知道该表中的列名,以显示整个SQL表...,你应该使用第二个选项:

<?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例子:

你有你的脚本的问题:

  • ...类的DateTime对象的陈述不能转换成字符串......错误可能是echo "<td>".$c_row."</td>";
  • mysql_error()是从不同的PHP扩展功能。 使用sqlsrv_errors()代替
  • 你不需要调用current()next() PHP函数来获取数组中的每个项目。 使用foreach($array as $key => $value) { ... }代替。
  • 你需要调用sqlsrv_field_metadata()函数来获取现场信息。

你可以尝试用下面的例子中,这将产生你预期的输出:

<?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);
?>


文章来源: My table has two timestamp values and remaining are string value. I want to display entire table in html using php