云主机测评网云主机测评网云主机测评网

云主机测评网
www.yunzhuji.net

php 中row函数 _PHP

PHP中,row()函数通常用于从结果集中获取一行作为关联数组、数字数组或两者兼有,这个函数是PDOStatement类的一部分,用于处理数据库查询的结果。

(图片来源网络,侵删)

以下是一些使用row()函数的示例:

1、获取一行作为关联数组:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn>setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn>prepare("SELECT id, firstname, lastname FROM MyGuests");
    $stmt>execute();
    // 设置结果集为关联数组
    $result = $stmt>setFetchMode(PDO::FETCH_ASSOC);
    
    // 获取一行数据
    $row = $stmt>fetch(PDO::FETCH_ASSOC);
    print_r($row);
} catch(PDOException $e) {
    echo "Error: " . $e>getMessage();
}
$conn = null;
?>

2、获取一行作为数字数组:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn>setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn>prepare("SELECT id, firstname, lastname FROM MyGuests");
    $stmt>execute();
    // 设置结果集为数字数组
    $result = $stmt>setFetchMode(PDO::FETCH_NUM);
    
    // 获取一行数据
    $row = $stmt>fetch(PDO::FETCH_NUM);
    print_r($row);
} catch(PDOException $e) {
    echo "Error: " . $e>getMessage();
}
$conn = null;
?>

3、获取一行作为关联数组和数字数组:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn>setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn>prepare("SELECT id, firstname, lastname FROM MyGuests");
    $stmt>execute();
    // 设置结果集为关联数组和数字数组
    $result = $stmt>setFetchMode(PDO::FETCH_BOTH);
    
    // 获取一行数据
    $row = $stmt>fetch(PDO::FETCH_BOTH);
    print_r($row);
} catch(PDOException $e) {
    echo "Error: " . $e>getMessage();
}
$conn = null;
?>
打赏
版权声明:主机测评不销售、不代购、不提供任何支持,仅分享信息/测评(有时效性),自行辨别,请遵纪守法文明上网。
文章名称:《php 中row函数 _PHP》
文章链接:https://www.yunzhuji.net/xunizhuji/196660.html

评论

  • 验证码