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:147800browse

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:

<?php
header("Content-Type:text/html; charset=utf-8");    //设置页面的编码格式
$dbms = "mysql";                                  // 数据库的类型
$dbName ="php_cn";                                //使用的数据库名称
$user = "root";                                   //使用的数据库用户名
$pwd = "root";                                    //使用的数据库密码
$host = "localhost";                              //使用的主机名称
$dsn  = "$dbms:host=$host;dbname=$dbName";
try{
    $pdo=new PDO($dsn,$user,$pwd);//初始化一个PDO对象,就是创建了数据库连接对象$pdo
    $query="select * from user";//需要执行的sql语句
    $res=$pdo->prepare($query);//准备查询语句
    $res->execute();            //执行查询语句,并返回结果集
    ?>
    <table border="1" width="500">
        <tr>
            <td height="22" align="center" valign="middle" >ID(第一列,数据的ID值)</td>
        </tr>
        <tr>
        <td height="22" align="center" valign="middle"><?php echo $res -> fetchColumn(0);?></td>
        </tr>
            <tr>
                <td height="22" align="center" valign="middle"><?php echo $res -> fetchColumn(0);?></td>
            </tr>
            <tr>
                <td height="22" align="center" valign="middle"><?php echo $res -> fetchColumn(0);?></td>
            </tr>
<?php
    //}
}catch(Exception $e){
    die("Error!:".$e->getMessage().&#39;<br>&#39;);
}
?>
</table>

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