duanpu1111 2013-04-17 22:37
浏览 25

Php PDO错误的行数

Can someone explain me when i echo $rows_affected variable, why i only get 1 when I should be getting 2 based on my db records? More than half an hour trying to figure out this and still cant solve it.. Still noob on PDO.

Any other advices are welcome.

$sql1 = "SELECT title FROM page WHERE id=?";
        $stm = $db->prepare($sql1);
        $stm->execute(array($id));
        $row1 = $stm->fetch(PDO::FETCH_ASSOC);

        $sql2 = "SELECT id,title,pg_title FROM page WHERE title=? ORDER BY id ASC";
        $stm = $db->prepare($sql2);
        $stm->execute(array($row1['title']));

        $rows_affected = $stm->fetchColumn();

        if (count($rows_affected) > 0) {
            for($i=0; $i <= $rows_affected; $i++) {

                $row2 = $stm->fetch(PDO::FETCH_ASSOC);

                if($row2['id']==$id){
                    echo '<a href="javascript: void(0)"><b>'.$row2['pg_title'].'</b></a>&nbsp;&nbsp;';
                }else{
                    echo '<a href="page.php?id='.$row2['id'].'';?><?php if(isset($_GET['edit']) && $_GET['edit']==1){ echo '&edit=1';}?><?php echo'">'.$row2['pg_title'].'</a>&nbsp;&nbsp;';

                }
            }
        }
  • 写回答

3条回答 默认 最新

  • dongzhi5846 2013-04-17 22:50
    关注

    fetchColumn():

    Returns a single column from the next row of a result set or FALSE if there are no more rows.

    I think you want to use fetchAll(). Use PDO::FETCH_COLUMN as the $fetch_style if you only want the first column. E.g.

    $rows_affected = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
    
    评论

报告相同问题?

悬赏问题

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