Database Reference
In-Depth Information
AND ORDINAL_POSITION = 1"
;
$sth
=
$dbh
->
prepare
(
$stmt
);
$sth
->
execute
(
array
(
$db_name
,
$tbl_name
));
list
(
$sort_col
)
=
$sth
->
fetch
(
PDO
::
FETCH_NUM
);
}
# Construct query to select records from the table, sorting by the
# named column. Limit output to 50 rows to avoid dumping entire
# contents of large tables.
$stmt
=
sprintf
(
"SELECT * FROM %s.%s ORDER BY %s LIMIT 50"
,
quote_identifier
(
$db_name
),
quote_identifier
(
$tbl_name
),
quote_identifier
(
$sort_col
));
$sth
=
$dbh
->
query
(
$stmt
);
# Display query results as HTML table. Use query metadata to get column
# names, and display names in first row of table as hyperlinks that cause
# the table to be redisplayed, sorted by the corresponding table column.
print
(
'<table border="1">'
);
print
(
'<tr>'
);
$ncols
=
$sth
->
columnCount
();
for
(
$i
=
0
;
$i
<
$ncols
;
$i
++
)
{
$col_info
=
$sth
->
getColumnMeta
(
$i
);
$col_name
=
$col_info
[
'name'
];
printf
(
'<th><a href="%s?sort=%s">%s</a></th>'
,
$_SERVER
[
'PHP_SELF'
],
urlencode
(
$col_name
),
htmlspecialchars
(
$col_name
));
}
print
(
'</tr>'
);
while
(
$row
=
$sth
->
fetch
(
PDO
::
FETCH_NUM
))
{
print
(
'<tr>'
);
for
(
$i
=
0
;
$i
<
$ncols
;
$i
++
)
{
# encode values, using for empty cells
$val
=
$row
[
$i
];
if
(
isset
(
$val
)
&&
$val
!=
''
)
$val
=
htmlspecialchars
(
$val
);
else
$val
=
' '
;
printf
(
'<td>%s</td>'
,
$val
);
}
print
(
'</tr>'
);
}
print
(
'</table>'
);
$dbh
=
NULL
;
?>