Home  >  Article  >  Backend Development  >  A must-have method for querying data - setFetchMode () in php

A must-have method for querying data - setFetchMode () in php

autoload
autoloadOriginal
2021-03-12 11:38:332622browse

PDOStatement::fetch()

PDOStatement::fetch    ([ int $fetch_style   [, int $cursor_orientation = PDO::FETCH_ORI_NEXT   [, int $cursor_offset = 0  ]]] ) : mixed

$fetch_styleThis value must be one of the PDO::FETCH_* series of constants:

  • PDO::FETCH_ASSOC: Returns an array whose index is the result set column name

  • PDO::FETCH_BOTH (default): Returns an array whose index is the result set column name and the column number starting with 0 Array

  • ## PDO::FETCH_BOUND: Returns TRUE and assigns the column value in the result set to the PHP variable bound by the PDOStatement::bindColumn() method.

  • PDO::FETCH_CLASS: Returns a new instance of the request class, mapping the column names in the result set to the corresponding attribute names in the class. If fetch_style contains PDO::FETCH_CLASSTYPE (for example: PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE), the class name is determined by the value of the first column.

  • PDO::FETCH_INTO: Update an existing instance of the requested class, mapping the columns in the result set to the named attributes in the class

  • PDO::FETCH_LAZY: Use PDO::FETCH_BOTH and PDO::FETCH_OBJ in combination to create an object variable name for access

  • PDO::FETCH_NUM: Returns an index starting with 0 Array of result set column numbers

  • PDO::FETCH_OBJ: Returns an anonymous object whose attribute name corresponds to the result set column name


When using the fetch() method by default, we will get double data in the database:

A must-have method for querying data - setFetchMode () in php


But in the database, there is really only a simple piece of data:

A must-have method for querying data - setFetchMode () in php


If you only want to get a simple index as the result set column name, you only need to set it simply:

$statement->setFetchMode(PDO::FETCH_ASSOC);

A must-have method for querying data - setFetchMode () in php

Recommended:

php video tutorial

The above is the detailed content of A must-have method for querying data - setFetchMode () in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn