1

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

Zhorov
  • 28,486
  • 6
  • 27
  • 52
sruthi
  • 11
  • 1
  • 3
    Why are you referencing SQL Server functions (i.e. `sqlsrv_connect`) and MySQL functions (i.e. `mysql_error`)? Also, something like `$query = "SELECT * FROM ".$selected_table;` is a **huge** injection issue. – Thom A Aug 07 '19 at 10:36
  • Possible duplicate of [How do I get the MySQL table structure in PHP? Plus a list of all tables?](https://stackoverflow.com/questions/468458/how-do-i-get-the-mysql-table-structure-in-php-plus-a-list-of-all-tables) – Will Aug 07 '19 at 10:42

1 Answers1

0

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);
?>
Zhorov
  • 28,486
  • 6
  • 27
  • 52