dbx_query
(PHP 4 >= 4.0.6, PHP 5 < 5.1.0, PECL dbx >= 1.1.0)
dbx_query — Send a query and fetch all results (if any)
Description
Sends a query and fetch all results.
Parameters
-
link_identifier
-
The DBX link object returned by dbx_connect()
-
sql_statement
-
SQL statement.
Data inside the query should be properly escaped.
-
flags
-
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_RESULT_INDEX
-
It is always set, that is, the returned object
has a data property which is a 2 dimensional
array indexed numerically. For example, in the expression
data[2][3] 2 stands for the row
(or record) number and 3 stands for the column
(or field) number. The first row and column are indexed at 0.
If
DBX_RESULT_ASSOC
is also specified, the returning object contains the information related toDBX_RESULT_INFO
too, even if it was not specified. -
DBX_RESULT_INFO
- It provides info about columns, such as field names and field types.
-
DBX_RESULT_ASSOC
- It effects that the field values can be accessed with the respective column names used as keys to the returned object's data property. Associated results are actually references to the numerically indexed data, so modifying data[0][0] causes that data[0]['field_name_for_first_column'] is modified as well.
-
DBX_RESULT_UNBUFFERED
- This flag will not create the data property, and the rows property will initially be 0. Use this flag for large datasets, and use dbx_fetch_row() to retrieve the results row by row. The dbx_fetch_row() function will return rows that are conformant to the flags set with this query. Incidentally, it will also update the rows each time it is called.
-
DBX_COLNAMES_UNCHANGED
- The case of the returned column names will not be changed.
-
DBX_COLNAMES_UPPERCASE
- The case of the returned column names will be changed to uppercase.
-
DBX_COLNAMES_LOWERCASE
- The case of the returned column names will be changed to lowercase.
DBX_RESULT_INDEX
is always used, regardless of the actual value offlags
parameter. This means that only the following combinations are effective:-
DBX_RESULT_INDEX
-
DBX_RESULT_INDEX
|DBX_RESULT_INFO
-
DBX_RESULT_INDEX
|DBX_RESULT_INFO
|DBX_RESULT_ASSOC
- this is the default, ifflags
is not specified.
-
Return Values
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
:
- handle
-
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);
?> - cols and rows
-
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
?> - info (optional)
-
It is returned only if either
DBX_RESULT_INFO
orDBX_RESULT_ASSOC
is specified in theflags
parameter. It is a 2 dimensional array, that has two named rows (name and type) to retrieve column information.Example #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";
}
?> - data
-
This property contains the actual resulting data, possibly associated
with column names as well depending on
flags
. IfDBX_RESULT_ASSOC
is set, it is possible to use $result->data[2]["field_name"].Example #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";
?>Example #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";
?>
Examples
Example #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);
?>
Notes
Note:
Always refer to the module-specific documentation as well.
Column names for queries on an Oracle database are returned in lowercase.
See Also
- dbx_escape_string() - Escape a string so it can safely be used in an sql-statement
- dbx_fetch_row() - Fetches rows from a query-result that had the DBX_RESULT_UNBUFFERED flag set
- dbx_connect() - Open a connection/database
Vertaling niet beschikbaar
De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.
Als je de moed voelt, kun je je vertaling aanbieden ;-)
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-dbx-query.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.