doutiaosu2310 2013-05-04 20:54
浏览 58
已采纳

计算PDO中的行数

This function counts songs for specified album using PDO.

I tried looking at what's wrong but nothing. searched here and bing, nothing.

function artist_count_songs($id) {
    global $db;
    $count = $db->prepare("SELECT COUNT(`song_id`) FROM `songs` WHERE `album_id` = :id");
    $count = $count->execute(array(':id' => $id));        
    echo $count;
}

Is there something I'm missing? tried rowcolumn still nothing

Database structure

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

CREATE TABLE IF NOT EXISTS `songs` (
  `song_id` int(11) NOT NULL AUTO_INCREMENT,
  `album_id` int(11) NOT NULL,
  `name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
  `download_url` varchar(1024) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`song_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

INSERT INTO `songs` (`song_id`, `album_id`, `name`, `download_url`) VALUES
(1, 1, 'بحبك اه', 'downloads/tamerhosny/b7bk-ah.mp3'),
(2, 1, 'بحبك اه', 'downloads/tamerhosny/b7bk-ah.mp3');

EDIT 1

    // Count songs total for album
    function artist_count_songs($album_id) {
        global $db;        
        $stmt    = $db->prepare("SELECT COUNT(`song_id`) FROM `songs` WHERE `album_id` = :album_id");
        $success = $stmt->execute(array(':album_id' => $album_id));
        list($count) = $stmt->fetch();
        echo '<th>';
        echo $count;
        echo '</th>';
    }
  • 写回答

1条回答 默认 最新

  • duandao1931 2013-05-04 20:57
    关注

    $count is actually a boolean that is the return value of the execute method.

    $stmt    = $db->prepare(
        "SELECT COUNT(`song_id`) FROM `songs` WHERE `album_id` = :id"
    );
    var_dump($stmt);
    
    $success = $stmt->execute(array(':id' => $id));
    var_dump($success);
    
    list($count) = $stmt->fetch();
    var_dump($count);    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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