summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Regad <dregad@mantisbt.org>2024-03-22 21:10:52 +0100
committerDamien Regad <dregad@mantisbt.org>2024-03-22 22:14:03 +0100
commit6c7b6fee98a19a2cb64d2b05cfd02f9e2c8ebe98 (patch)
treef262f7e084e267df8c6c3d0df55b22cedeff0413
parent172489c55d634b5d01b1f32c8717949784c23396 (diff)
downloadadodb-6c7b6fee98a19a2cb64d2b05cfd02f9e2c8ebe98.tar.gz
adodb-6c7b6fee98a19a2cb64d2b05cfd02f9e2c8ebe98.tar.bz2
adodb-6c7b6fee98a19a2cb64d2b05cfd02f9e2c8ebe98.zip
Fix getAssoc() with ADODB_FETCH_DEFAULT mode
With mysqli (and possibly with other drivers where driver-specific fetchMode values are different from ADOdb's as well, but this has not been tested), getAssoc() would not return the expected key=>value pairs when fetch mode was set to ADODB_FETCH_DEFAULT. We now use ADORecordSet::$adodbFetchMode property as reference fetch mode instead of the driver-specific ADOConnection::$fetchMode (which is often not set), and check it against both ADODB_FETCH_BOTH and ADODB_FETCH_DEFAULT. Fixes #1023
-rw-r--r--adodb.inc.php8
-rw-r--r--t2.php68
2 files changed, 73 insertions, 3 deletions
diff --git a/adodb.inc.php b/adodb.inc.php
index 7683c8e1..fce37df0 100644
--- a/adodb.inc.php
+++ b/adodb.inc.php
@@ -4300,6 +4300,8 @@ class ADORecordSet implements IteratorAggregate {
*/
function getAssoc($force_array = false, $first2cols = false)
{
+ global $ADODB_FETCH_MODE;
+
/*
* Insufficient rows to show data
*/
@@ -4322,8 +4324,8 @@ class ADORecordSet implements IteratorAggregate {
* Get the fetch mode when the call was executed, this may be
* different than ADODB_FETCH_MODE
*/
- $fetchMode = $this->connection->fetchMode;
- if ($fetchMode == ADODB_FETCH_BOTH) {
+ $fetchMode = $this->adodbFetchMode;
+ if ($fetchMode == ADODB_FETCH_BOTH || $fetchMode == ADODB_FETCH_DEFAULT) {
/*
* If we are using BOTH, we present the data as if it
* was in ASSOC mode. This could be enhanced by adding
@@ -4355,7 +4357,7 @@ class ADORecordSet implements IteratorAggregate {
$myFields = $this->fields;
- if ($fetchMode == ADODB_FETCH_BOTH) {
+ if ($fetchMode == ADODB_FETCH_BOTH || $fetchMode == ADODB_FETCH_DEFAULT) {
/*
* extract the associative keys
*/
diff --git a/t2.php b/t2.php
new file mode 100644
index 00000000..8542021b
--- /dev/null
+++ b/t2.php
@@ -0,0 +1,68 @@
+<?php
+include 'adodb.inc.php';
+
+// connect
+$db = ADONewConnection("mysqli");
+$db->connect("localhost", "root", "password", "test");
+
+// output some details
+$fetchModeMap = [
+ ADODB_FETCH_DEFAULT => "default (driver specific)",
+ ADODB_FETCH_NUM => "numeric",
+ ADODB_FETCH_ASSOC => "associative",
+ ADODB_FETCH_BOTH => "both",
+];
+
+$ADODB_FETCH_MODE = (int)$argv[1] ?? ADODB_FETCH_DEFAULT;
+
+$extension_yn = !empty($GLOBALS["ADODB_EXTENSION"]) ? "yes" : "no";
+$php_version = phpversion();
+echo "extension: {$extension_yn}\n";
+echo "fetchmode: {$fetchModeMap[$GLOBALS["ADODB_FETCH_MODE"]]}\n";
+echo "php version: {$php_version}\n\n";
+
+// temp table for example
+$tmp_table = $db->execute("
+ CREATE TEMPORARY TABLE `lookup_example` (
+ `name` VARCHAR(45) NOT NULL,
+ `value` VARCHAR(45) NULL,
+ `id` INT NOT NULL AUTO_INCREMENT,
+ PRIMARY KEY (`id`)
+ ) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4
+");
+
+// populate temp table with dummy results
+foreach ([
+ "NAME1" => "Name1 description",
+ "NAME2" => "Name2 description",
+ "NAME3" => "Name3 description",
+ ] as $name => $value) {
+ $db->execute("INSERT INTO `lookup_example` (`name`, `value`) VALUES ({$db->qstr($name)}, {$db->qstr($value)})");
+}
+
+// normal execution and access of associative values
+$results = $db->execute("SELECT `name`, `value` FROM `lookup_example`");
+
+print_r($results->getRows());
+foreach ($results as $result) {
+ if (array_key_exists('name', $result)) {
+ $name = $result['name'];
+ $value = $result['value'];
+ }
+ else {
+ [$name, $value] = $result;
+ }
+
+ echo "{$name}: {$value}\n";
+}
+
+// move back to start and pull associative on recordset
+$results->move(0);
+echo "\n";
+print_r($results->getAssoc(false, false));
+$results->move(0);
+print_r($results->getAssoc(false, true));
+$results->move(0);
+print_r($results->getAssoc(true, false));
+$results->move(0);
+print_r($results->getAssoc(true, true));