【问题标题】:Remind first row after getting all data - PHP PDO [duplicate]获取所有数据后提醒第一行 - PHP PDO [重复]
【发布时间】:2018-03-11 13:36:14
【问题描述】:

我正在使用 php PDO 进行服务器连接。就这样

$stmt = $pdo->prepare('call naviSql( ?, ?, ? )');
$stmt->execute( array( $someone, $on_navi, $on_date ) );

while( $row = $stmt->fetch(  )  ) { 

    echo '<a href="http://'.$_SERVER['HTTP_HOST'].'/anbu/home.php?topic='.$row['topic_name'].'" class="list-group-item list-group-item-action justify-content-between"></a>';
}

现在回显所有数据后,如何再次提醒第一行值?

【问题讨论】:

    标签: php pdo


    【解决方案1】:

    改用fetchAll 并将所有结果存储在一个变量中。然后可以通过其索引访问第一行,即:

    // Store all fetched rows in a variable
    $results = $stmt->fetchAll();
    
    // Iterate through all your results
    foreach($results as $row) {
        echo '<a href="http://'.$_SERVER['HTTP_HOST'].'/anbu/home.php?topic='.$row['topic_name'].'" class="list-group-item list-group-item-action justify-content-between"></a>';
    }
    
    // Re-access the first row
    $firstRow = $results[0];
    print_r($firstRow);
    

    【讨论】:

    • 你能告诉我如何从另一个数组内的数组中获取数据吗? $first = $results[$row['topic_name']]??
    • @kai 那将是$results[0]['topic_name']
    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 2023-04-06
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2017-03-30
    相关资源
    最近更新 更多