Home  >  Article  >  Backend Development  >  PDO::query怎么理解

PDO::query怎么理解

PHPz
PHPzOriginal
2016-06-06 20:23:131412browse

PDO::query怎么理解

PDO::query的理解是

PDO::query — 执行 SQL 语句,返回PDOStatement对象,可以理解为结果集(PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)

说明

语法

public PDOStatement PDO::query ( string $statement )
public PDOStatement PDO::query ( string $statement , int $PDO::FETCH_COLUMN , int $colno )
public PDOStatement PDO::query ( string $statement , int $PDO::FETCH_CLASS , string $classname , array $ctorargs )
public PDOStatement PDO::query ( string $statement , int $PDO::FETCH_INTO , object $object )

PDO::query() 在一个单独的函数中调用并执行 SQL 语句, 返回结果集 (如果有),语句作为一个PDOStatement对象返回。

参数

statement

要执行的SQL语句。

返回值

如果成功,PDO::query()返回PDOStatement对象,如果失败返回 FALSE 。

实例

PDO::query实例

遍历输出结果集:

<?php
function getFruit($conn) {
    $sql = &#39;SELECT name, color, calories FROM fruit ORDER BY name&#39;;
    foreach ($conn->query($sql) as $row) {
        print $row[&#39;name&#39;] . "\t";
        print $row[&#39;color&#39;] . "\t";
        print $row[&#39;calories&#39;] . "\n";
    }
}
?>

以上输出结果为:

apple   red     150
banana  yellow  250
kiwi    brown   75
lemon   yellow  25
orange  orange  300
pear    green   150
watermelon      pink    90

更多相关知识,请访问PHP中文网

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