duannaoben8011 2016-02-08 18:35 采纳率: 0%
浏览 57

使用PDO在SQLite中检索单个(也是唯一)行的最简单方法

I have this PDO

$stmt = $db->prepare('SELECT * FROM channels WHERE id=:id');
$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
$result = $stmt->execute();

What now is the simplest method to access the row returned using $row['column_name']?

I know that for sure the query will only ever return a single row as it's impossible to have more than 1 row with the same id so I am looking to keep the code as simple as possible to access that row.

Examples I've seen online are quite long and complex, using while loops to loop through rows etc. that I don't need.

  • 写回答

1条回答 默认 最新

  • dongneng5383 2016-02-08 18:44
    关注

    It should be as simple as

    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    

    The examples you've seen probably have something more like

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ...
    

    because they're designed to process a query result with multiple rows. But if you know you'll only ever have one row, you can just forgo the loop. Of course, this is assuming your query actually executed successfully and returned a result. It would be a good idea to at least check

    if ($row) { ...
    

    before you try to use it in your subsequent code.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀