Home  >  Article  >  Backend Development  >  Detailed explanation of the fetchColumn() method for obtaining result sets in PDO

Detailed explanation of the fetchColumn() method for obtaining result sets in PDO

黄舟
黄舟Original
2017-04-28 17:40:147641browse

Detailed explanation of the fetchColumn() method for obtaining the result set in PDO

The fetchCloumn() method is to obtain the value of the specified column in the next row of the result set, from the result set The next row of returns a single column.

Then we have the previous two articles "Detailed explanation of the fetchAll() method for obtaining the result set in PDO" and "Detailed explanation of the fetch() method for obtaining the result set in PDO》We introduced two similar methods of obtaining result sets in PDO, so let’s introduce the fetchColumn() method to you next!

Let’s first look at the syntax format of the fetchColumn() method as follows:

string PDOStatement::fetchColumn ([ int $column_number = 0 ] )

Return a single column from the next row in the result set. If there is no more, return FALSE.

The optional parameter column_number sets the index value of the column in the row. The value starts from 0. If this parameter is omitted, the value will start from column 1.

Get the value of the specified column in the next row in the result set through the fetchColumn() method. Note: Here is "the value of the specified column in the next row in the result set"!

Below we use examples to introduce the specific usage of the fetchColumn() method. The specific steps are as follows:

First create a php file, design the web page, connect to the MySQL database through PDO, and then define the SELECT query statement, use the prepare() and execute() methods to perform the query operation, and then use the fetchColumn() method to output the first column value of the next row in the result set, which is the ID value of the output data. The specific code is as follows:

prepare($query);//准备查询语句
    $res->execute();            //执行查询语句,并返回结果集
    ?>
    getMessage().'
'); } ?>
ID(第一列,数据的ID值)
fetchColumn(0);?>
fetchColumn(0);?>
fetchColumn(0);?>

The output results are as follows:

Detailed explanation of the fetchColumn() method for obtaining result sets in PDO

We have finished introducing the three methods of obtaining result sets in PDO. I believe that friends have read it and obtained the results of PDO. If you have a certain understanding and understanding of the collection, you can contact us to introduce the three methods. Now I will start to introduce how to use PDO to capture errors in SQL statements. For details, please read "Use default mode-PDO:: ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)》!

The above is the detailed content of Detailed explanation of the fetchColumn() method for obtaining result sets in PDO. 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