PDOStatement::fetchColumn - Manual - PHP

update page now
  • Downloads
  • Documentation
  • Get Involved
  • Help
  • PHP 8.5
Search docs Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box PDOStatement::fetchObject » « PDOStatement::fetchAll
  • Manuel PHP
  • Référence des fonctions
  • Extensions sur les bases de données
  • Interface d'abstraction
  • PDO
  • PDOStatement
Change language: English German Spanish French Italian Japanese Brazilian Portuguese Russian Turkish Ukrainian Chinese (Simplified) Other PDOStatement::fetchColumn

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.9.0)

PDOStatement::fetchColumn Retourne une colonne depuis la ligne suivante d'un jeu de résultats

Description

public PDOStatement::fetchColumn(int $column = 0): mixed

Retourne une colonne depuis la ligne suivante d'un jeu de résultats ou false s'il n'y a plus de ligne.

Note:

PDOStatement::fetchColumn() ne doit pas être utilisé pour récupérer des colonnes contenant des booléens, car il n'est pas possible de distinguer une valeur false d'un retour avec aucune ligne à récupérer. Utilisez PDOStatement::fetch() à la place.

Liste de paramètres

column

Numéro de la colonne que vous voulez récupérer depuis la ligne (commençant à 0). Si aucune valeur n'est fournie, PDOStatement::fetchColumn() récupérera la première colonne.

Valeurs de retour

PDOStatement::fetchColumn() retourne une colonne depuis la ligne suivante d'un jeu de résultats ou false s'il n'y a plus de lignes.

Avertissement

Il n'y a pas de solution pour retourner une autre colonne depuis la même ligne si vous utilisez la fonction PDOStatement::fetchColumn() pour récupérer les données.

Erreurs / Exceptions

Émet une erreur de niveau E_WARNING si l'attribut PDO::ATTR_ERRMODE est défini à PDO::ERRMODE_WARNING.

Lève une exception PDOException si l'attribut PDO::ATTR_ERRMODE est défini à PDO::ERRMODE_EXCEPTION.

Exemples

Exemple #1 Retourne la première colonne de la ligne suivante

<?php$sth = $dbh->prepare("SELECT nom, couleur FROM fruit");$sth->execute();/* Récupère la première colonne depuis la première ligne d'un jeu de résultats */print "Récupère la première colonne depuis la première ligne d'un jeu de résultats :\n";$result = $sth->fetchColumn();print "nom=$result\n");print "Récupère la deuxième colonne depuis la seconde ligne d'un jeu de résultats :\n";$result = $sth->fetchColumn(1);print "couleur=$result\n";?>

L'exemple ci-dessus va afficher :

Récupère la première colonne depuis la première ligne d'un jeu de résultats : nom=lemon Récupère la deuxième colonne depuis la seconde ligne d'un jeu de résultats : couleur=orange

Voir aussi

  • PDO::query() - Prépare et Exécute une requête SQL sans marque substitutive
  • PDOStatement::fetch() - Récupère la ligne suivante d'un jeu de résultats PDO
  • PDOStatement::fetchAll() - Récupère les lignes restantes d'un ensemble de résultats
  • PDO::prepare() - Prépare une requête à l'exécution et retourne un objet
  • PDOStatement::setFetchMode() - Définit le mode de récupération par défaut pour cette requête

Found A Problem?

Learn How To Improve This Page • Submit a Pull Request • Report a Bug +add a note

User Contributed Notes 3 notes

up down 55 PhoneixSegovia at GOOGLE_MAIL_SERVER dot com15 years ago fetchColumn return boolean false when a row not is found or don't had more rows. up down 14 seanferd at assmasterdonkeyranch dot com18 years ago This is an excellent method for returning a column count. For example: <?php $db = new PDO('mysql:host=localhost;dbname=pictures','user','password'); $pics = $db->query('SELECT COUNT(id) FROM pics'); $this->totalpics = $pics->fetchColumn(); $db = null; ?> In my case $pics->fetchColumn() returns 641 because that is how many pictures I have in my db. up down -2 theking2(at)king.ma1 year ago When migrating from mysqli it is important that while mysqli_result::fetch_column will iterate over subsequent rows PDOStatement::fetchColumn will NOT! <?php while ( $row0 = $db->query("SELECT `value` FROM `bool`")->fetchColumn(0) ) { var_dump( $row0 ); } ?> is an endless loop unless the first column in the first row of the table bool is "0". +add a note
  • PDOStatement
    • bindColumn
    • bindParam
    • bindValue
    • closeCursor
    • columnCount
    • debugDumpParams
    • errorCode
    • errorInfo
    • execute
    • fetch
    • fetchAll
    • fetchColumn
    • fetchObject
    • getAttribute
    • getColumnMeta
    • getIterator
    • nextRowset
    • rowCount
    • setAttribute
    • setFetchMode
To Top ↑ and ↓ to navigate • Enter to select • Esc to close • / to open Press Enter without selection to search using Google

Tag » Colonne Pfp