问题
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:
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 totrue. By default this option isfalse.
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. Usesqlsrv_errors()instead- you do not need to call
current()andnext()PHP functions to get each item in an array. Useforeach($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);
?>
来源:https://stackoverflow.com/questions/57392400/my-table-has-two-timestamp-values-and-remaining-are-string-value-i-want-to-disp