(PHP 4 >= 4.0.6, PHP 5 <= 5.0.5, PECL dbx >= 1.1.0)
dbx_query — Send a query and fetch all results (if any)
Sends a query and fetch all results.
The DBX link object returned by dbx_connect()
SQL statement.
Data inside the query should be properly escaped.
The flags parameter is used to control the amount of information that is returned. It may be any combination of the following constants with the bitwise OR operator (|). The DBX_COLNAMES_* flags override the dbx.colnames_case setting from php.ini.
dbx_query() returns an object or 1 on success, and 0 on failure. The result object is returned only if the query given in sql_statement produces a result set (i.e. a SELECT query, even if the result set is empty).
The returned object has four or five properties depending on flags:
It is a valid handle for the connected database, and as such it can be used in module specific functions (if required).
<?php
$result = dbx_query($link, "SELECT id FROM table");
mysql_field_len($result->handle, 0);
?>
These contain the number of columns (or fields) and rows (or records) respectively.
<?php
$result = dbx_query($link, 'SELECT id FROM table');
echo $result->rows; // number of records
echo $result->cols; // number of fields
?>
Пример #1 lists each field's name and type
<?php
$result = dbx_query($link, 'SELECT id FROM table',
DBX_RESULT_INDEX | DBX_RESULT_INFO);
for ($i = 0; $i < $result->cols; $i++ ) {
echo $result->info['name'][$i] . "\n";
echo $result->info['type'][$i] . "\n";
}
?>
Пример #2 outputs the content of data property into HTML table
<?php
$result = dbx_query($link, 'SELECT id, parentid, description FROM table');
echo "<table>\n";
foreach ($result->data as $row) {
echo "<tr>\n";
foreach ($row as $field) {
echo "<td>$field</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
Пример #3 How to handle UNBUFFERED queries
<?php
$result = dbx_query ($link, 'SELECT id, parentid, description FROM table', DBX_RESULT_UNBUFFERED);
echo "<table>\n";
while ($row = dbx_fetch_row($result)) {
echo "<tr>\n";
foreach ($row as $field) {
echo "<td>$field</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
Версия | Описание |
---|---|
5.0.0 | Introduced DBX_RESULT_UNBUFFERED. |
4.3.0 | Introduced DBX_COLNAMES_UNCHANGED, DBX_COLNAMES_UPPERCASE, and DBX_COLNAMES_LOWERCASE. |
Пример #4 How to handle the returned value
<?php
$link = dbx_connect(DBX_ODBC, "", "db", "username", "password")
or die("Could not connect");
$result = dbx_query($link, 'SELECT id, parentid, description FROM table');
if (is_object($result) ) {
// ... do some stuff here, see detailed examples below ...
// first, print out field names and types
// then, draw a table filled with the returned field values
} else {
exit("Query failed");
}
dbx_close($link);
?>
Замечание:
Always refer to the module-specific documentation as well.
Column names for queries on an Oracle database are returned in lowercase.