PDO fetchColumn obtaining the result set () method Detailed

fetchCloumn () method is to obtain the result set value for the next line of the designated column, a separate return a result set from the next row. Linear motor brand

So we have in the previous two articles "Getting fetchAll result set of a PDO () method explanation" and "PDO acquire fetch the result set () method explain" we introduced two ways to get a similar result set in the PDO, so we are going to introduce fetchColumn () method with you!

Here we look at the syntax of the method under fetchColumn () as follows:

1

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

From the result set returned the next row an individual, if not the, FALSE is returned.

Optional parameter index value column_number column line set, from the value of zero, then If omitted, the value will start from the first column.

Get the value of the specified column in the result set by fetchColumn () method of the next line, Note: this is "in the next row in the result set of the specified column"!

Here we introduce specific uses examples to the specific usage fetchColumn () method, the following steps:

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

<?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().'<br>');

}

?>

</table>

Guess you like

Origin www.cnblogs.com/furuihua/p/12172786.html