Php mysql fetch one column

mysqli_result::fetch_column

Fetches one row of data from the result set and returns the 0-indexed column. Each subsequent call to this function will return the value from the next row within the result set, or false if there are no more rows.

Note: This function sets NULL fields to the PHP null value.

Parameters

0-indexed number of the column you wish to retrieve from the row. If no value is supplied, the first column will be returned.

Return Values

Returns a single column from the next row of a result set or false if there are no more rows.

There is no way to return another column from the same row if you use this function to retrieve data.

Examples

Example #1 mysqli_result::fetch_column() example

 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); $query = "SELECT CountryCode, Name FROM City ORDER BY ID DESC LIMIT 5"; $result = $mysqli->query($query); /* fetch a single value from the second column */ while ($Name = $result->fetch_column(1)) < printf("%s\n", $Name); >
 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); $query = "SELECT CountryCode, Name FROM City ORDER BY ID DESC LIMIT 5"; $result = mysqli_query($mysqli, $query); /* fetch a single value from the second column */ while ($Name = mysqli_fetch_column($result, 1)) < printf("%s\n", $Name); >

The above examples will output something similar to:

Rafah Nablus Jabaliya Hebron Khan Yunis

See Also

  • mysqli_fetch_all() — Fetch all result rows as an associative array, a numeric array, or both
  • mysqli_fetch_array() — Fetch the next row of a result set as an associative, a numeric array, or both
  • mysqli_fetch_assoc() — Fetch the next row of a result set as an associative array
  • mysqli_fetch_object() — Fetch the next row of a result set as an object
  • mysqli_fetch_row() — Fetch the next row of a result set as an enumerated array
  • mysqli_data_seek() — Adjusts the result pointer to an arbitrary row in the result
PHP 8.2

(PHP 5, 7, 8) mysqli_result::fetch_array mysqli_fetch_array the next row of set as an associative, numeric or both Object-oriented style Procedural style

(PHP 5, 7, 8) mysqli_result::fetch_assoc mysqli_fetch_assoc the next row of set an associative array Object-oriented style Procedural style Fetches one

(PHP 5, 7, 8) mysqli_result::fetch_field mysqli_fetch_field Returns the next in set Object-oriented style Procedural style Returns the definition of one

(PHP 5, 7, 8) mysqli_result::fetch_field_direct mysqli_fetch_field_direct meta-data for single Object-oriented style Procedural style Returns an object

Источник

PDOStatement::fetchColumn

Returns a single column from the next row of a result set or false if there are no more rows.

Note:

PDOStatement::fetchColumn() should not be used to retrieve boolean columns, as it is impossible to distinguish a value of false from there being no more rows to retrieve. Use PDOStatement::fetch() instead.

Parameters

0-indexed number of the column you wish to retrieve from the row. If no value is supplied, PDOStatement::fetchColumn() fetches the first column.

Return Values

PDOStatement::fetchColumn() returns a single column from the next row of a result set or false if there are no more rows.

There is no way to return another column from the same row if you use PDOStatement::fetchColumn() to retrieve data.

Errors/Exceptions

Emits an error with level E_WARNING if the attribute PDO::ATTR_ERRMODE is set to PDO::ERRMODE_WARNING .

Throws a PDOException if the attribute PDO::ATTR_ERRMODE is set to PDO::ERRMODE_EXCEPTION .

Examples

Example #1 Return first column of the next row

$sth = $dbh -> prepare ( «SELECT name, colour FROM fruit» );
$sth -> execute ();

print( «Fetch the first column from the first row in the result set:\n» );
$result = $sth -> fetchColumn ();
print( «name = $result \n» );

print( «Fetch the second column from the second row in the result set:\n» );
$result = $sth -> fetchColumn ( 1 );
print( «colour = $result \n» );
?>

The above example will output:

Fetch the first column from the first row in the result set: name = lemon Fetch the second column from the second row in the result set: colour = red

See Also

  • PDO::query() — Prepares and executes an SQL statement without placeholders
  • PDOStatement::fetch() — Fetches the next row from a result set
  • PDOStatement::fetchAll() — Fetches the remaining rows from a result set
  • PDO::prepare() — Prepares a statement for execution and returns a statement object
  • PDOStatement::setFetchMode() — Set the default fetch mode for this statement

Источник

mysqli_result::fetch_column

Выбирает одну строку данных из набора результатов и возвращает столбец с нулевым индексом. Каждый последующий вызов этой функции будет возвращать значение из следующей строки в наборе результатов или false , если строк больше нет.

Примечание . Эта функция устанавливает для полей NULL null значение PHP .

Parameters

0-индексированный номер столбца,который вы хотите получить из строки.Если значение не указано,будет возвращен первый столбец.

Return Values

Возвращает один столбец из следующей строки набора результатов или false , если строк больше нет.

Нет возможности вернуть другой столбец из той же строки,если вы используете эту функцию для получения данных.

Examples

Пример # 1 mysqli_result :: fetch_column () Пример

 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); $query = "SELECT CountryCode, Name FROM City ORDER BY ID DESC LIMIT 5"; $result = $mysqli->query($query); /* fetch a single value from the second column */ while ($Name = $result->fetch_column(1)) < printf("%s\n", $Name); >
 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); $query = "SELECT CountryCode, Name FROM City ORDER BY ID DESC LIMIT 5"; $result = mysqli_query($mysqli, $query); /* fetch a single value from the second column */ while ($Name = mysqli_fetch_column($result, 1)) < printf("%s\n", $Name); >

Из приведенных выше примеров будет выведено нечто похожее:

Rafah Nablus Jabaliya Hebron Khan Yunis

See Also

  • mysqli_fetch_all () — Извлекает все строки результатов в виде ассоциативного массива, числового массива или обоих
  • mysqli_fetch_array () — Извлекает следующую строку набора результатов как ассоциативный, числовой массив или и то, и другое
  • mysqli_fetch_assoc () — Извлекает следующую строку набора результатов как ассоциативный массив
  • mysqli_fetch_object () — Извлекает следующую строку результирующего набора как объект
  • mysqli_fetch_row () — Извлекает следующую строку результирующего набора как нумерованный массив
  • mysqli_data_seek () — Настраивает указатель результата на произвольную строку в результате
PHP 8.2

(PHP 5,7,8)mysqli_result::fetch_array mysqli_fetch_array следующая строка набора в ассоциативном,числовом или обоих вариантах Объектно-ориентированный стиль Процедурный стиль

(PHP 5,7,8)mysqli_result::fetch_assoc mysqli_fetch_assoc следующая строка набора ассоциативного массива Объектно-ориентированный стиль Процедурный стиль Извлекает один

(PHP 5,7,8)mysqli_result::fetch_field mysqli_fetch_field Возвращает следующее в наборе Объектно-ориентированный стиль Процедурный стиль Возвращает определение одного

(PHP 5,7,8)mysqli_result::fetch_field_direct mysqli_fetch_field_direct мета-данные для одного Объектно-ориентированный стиль Процедурный стиль Возвращает объект

Источник

Читайте также:  Скрипты php сервера файлов
Оцените статью