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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测