Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,9 @@ PHP 8.1 UPGRADE NOTES
. Binding in execute has been added to mysqli prepared statements.
Parameters can now be passed to mysqli_stmt::execute as an array.
RFC: https://wiki.php.net/rfc/mysqli_bind_in_execute
. A new function has been added to mysqli_result called mysqli_fetch_column().
It allows for fetching single scalar values from the result set.
RFC: https://wiki.php.net/rfc/mysqli_fetch_column

- PDO MySQL:
. The PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY attribute has been added, which
Expand Down
7 changes: 7 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -426,6 +426,11 @@ public function fetch_object(string $class = "stdClass", array $constructor_args
*/
public function fetch_row() {}

/**
* @alias mysqli_fetch_column
*/
public function fetch_column(int $column = 0): null|int|float|string|false {}

/**
* @return bool
* @alias mysqli_field_seek
Expand DownExpand Up@@ -664,6 +669,8 @@ function mysqli_fetch_object(mysqli_result $result, string $class = "stdClass",

function mysqli_fetch_row(mysqli_result $result): array|null|false {}

function mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false {}

function mysqli_field_count(mysqli $mysql): int {}

function mysqli_field_seek(mysqli_result $result, int $index): bool {}
Expand Down
14 changes: 13 additions & 1 deletion ext/mysqli/mysqli_arginfo.h
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
* Stub hash: 42fdb807a547cfa3ba35bb2b8a4ee0f0d556a18c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand DownExpand Up@@ -115,6 +115,11 @@ ZEND_END_ARG_INFO()

#define arginfo_mysqli_fetch_row arginfo_mysqli_fetch_assoc

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_fetch_column, 0, 1, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, result, mysqli_result, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_mysqli_field_count arginfo_mysqli_errno

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mysqli_field_seek, 0, 2, _IS_BOOL, 0)
Expand DownExpand Up@@ -609,6 +614,10 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_fetch_row arginfo_class_mysqli_character_set_name

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_mysqli_result_fetch_column, 0, 0, MAY_BE_NULL|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, column, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_result_field_seek arginfo_class_mysqli_result_fetch_field_direct

#define arginfo_class_mysqli_result_free_result arginfo_class_mysqli_character_set_name
Expand DownExpand Up@@ -709,6 +718,7 @@ ZEND_FUNCTION(mysqli_fetch_array);
ZEND_FUNCTION(mysqli_fetch_assoc);
ZEND_FUNCTION(mysqli_fetch_object);
ZEND_FUNCTION(mysqli_fetch_row);
ZEND_FUNCTION(mysqli_fetch_column);
ZEND_FUNCTION(mysqli_field_count);
ZEND_FUNCTION(mysqli_field_seek);
ZEND_FUNCTION(mysqli_field_tell);
Expand DownExpand Up@@ -833,6 +843,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(mysqli_fetch_assoc, arginfo_mysqli_fetch_assoc)
ZEND_FE(mysqli_fetch_object, arginfo_mysqli_fetch_object)
ZEND_FE(mysqli_fetch_row, arginfo_mysqli_fetch_row)
ZEND_FE(mysqli_fetch_column, arginfo_mysqli_fetch_column)
ZEND_FE(mysqli_field_count, arginfo_mysqli_field_count)
ZEND_FE(mysqli_field_seek, arginfo_mysqli_field_seek)
ZEND_FE(mysqli_field_tell, arginfo_mysqli_field_tell)
Expand DownExpand Up@@ -998,6 +1009,7 @@ static const zend_function_entry class_mysqli_result_methods[] = {
ZEND_ME_MAPPING(fetch_assoc, mysqli_fetch_assoc, arginfo_class_mysqli_result_fetch_assoc, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_object, mysqli_fetch_object, arginfo_class_mysqli_result_fetch_object, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_row, mysqli_fetch_row, arginfo_class_mysqli_result_fetch_row, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(fetch_column, mysqli_fetch_column, arginfo_class_mysqli_result_fetch_column, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(field_seek, mysqli_field_seek, arginfo_class_mysqli_result_field_seek, ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(free_result, mysqli_free_result, arginfo_class_mysqli_result_free_result, ZEND_ACC_PUBLIC)
ZEND_ME(mysqli_result, getIterator, arginfo_class_mysqli_result_getIterator, ZEND_ACC_PUBLIC)
Expand Down
33 changes: 33 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -436,6 +436,39 @@ PHP_FUNCTION(mysqli_fetch_assoc)
}
/* }}} */

/* {{{ Fetch a column from the result row */
PHP_FUNCTION(mysqli_fetch_column)
{
MYSQL_RES *result;
zval *mysql_result;
zval row_array;
zend_long col_no = 0;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
RETURN_THROWS();
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);

if (col_no < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}
if (col_no >= mysql_num_fields(result)) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
RETURN_THROWS();
}

php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
if (Z_TYPE(row_array) != IS_ARRAY) {
zval_ptr_dtor_nogc(&row_array);
RETURN_FALSE;
}

ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
zval_ptr_dtor_nogc(&row_array);
}
/* }}} */

/* {{{ Fetches all result rows as an associative array, a numeric array, or both */
PHP_FUNCTION(mysqli_fetch_all)
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ require_once('skipifconnectfailure.inc');
'fetch_fields' => true,
'fetch_object' => true,
'fetch_row' => true,
'fetch_column' => true,
'field_seek' => true,
'free' => true,
'free_result' => true,
Expand Down
143 changes: 143 additions & 0 deletions ext/mysqli/tests/mysqli_fetch_column.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
--TEST--
mysqli_fetch_column()
--SKIPIF--
<?php
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require_once "connect.inc";
require 'table.inc';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");

print "[001]\n";
var_dump(mysqli_fetch_column($res));

print "[002]\n";
var_dump(mysqli_fetch_column($res));


$link->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[003]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a
");
print "[004]\n";
var_dump(mysqli_fetch_column($res, 1));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3
");
print "[005]\n";
var_dump(mysqli_fetch_column($res, 2));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d
");
print "[006]\n";
var_dump(mysqli_fetch_column($res, 3));

$res = mysqli_query($link, "SELECT
1 AS a,
2 AS a,
3,
NULL AS d,
true AS e
");
print "[007]\n";
var_dump(mysqli_fetch_column($res, 4));

$res = mysqli_query($link, "SELECT
1.42 AS a
");
print "[008]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT
1.42E0 AS a
");
print "[009]\n";
var_dump(mysqli_fetch_column($res, 0));

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[010]\n";
try {
var_dump(mysqli_fetch_column($res, -1));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1");
print "[011]\n";
try {
var_dump(mysqli_fetch_column($res, 2));
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

mysqli_free_result($res);
try {
mysqli_fetch_column($res);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

$res = $link->query("SELECT id, label FROM test ORDER BY id LIMIT 2");

print "[012]\n";
var_dump($res->fetch_column());

print "[013]\n";
var_dump($res->fetch_column(1));

mysqli_close($link);
?>
--CLEAN--
<?php
require_once "clean_table.inc";
?>
--EXPECT--
[001]
string(1) "1"
[002]
bool(false)
[003]
int(1)
[004]
int(2)
[005]
int(3)
[006]
NULL
[007]
int(1)
[008]
string(4) "1.42"
[009]
float(1.42)
[010]
mysqli_fetch_column(): Argument #2 ($column) must be greater than or equal to 0
[011]
mysqli_fetch_column(): Argument #2 ($column) must be less than the number of fields for this result set
mysqli_result object is already closed
[012]
int(1)
[013]
string(1) "b"