0

Mysqlの構造と日付:

--
-- Table structure for table `site_links`
--

CREATE TABLE IF NOT EXISTS `site_links` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(15) NOT NULL,
  `link_title` varchar(255) NOT NULL,
  `link_url` text NOT NULL COMMENT,
  `status` tinyint(1) NOT NULL DEFAULT '0',
  `views_count` int(11) unsigned NOT NULL DEFAULT '0',
  `unlocks_count` int(11) unsigned NOT NULL DEFAULT '0',
  `report_count` int(11) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=74 ;

--
-- Dumping data for table `site_links`
--

INSERT INTO `site_links` (`id`, `username`, `link_title`, `link_url`, `status`, `views_count`, `unlocks_count`, `report_count`) VALUES
(56, 'john', 't1', 'http://google.com', 1, 4, 0, 0),
(57, 'john', 't2', 'http://google.com', 1, 0, 0, 0),
(58, 'james', 't3', 'http://google.com', 1, 3, 0, 0),
(59, 'dave', 't4', 'http://google.com', 1, 8, 0, 0),
(60, 'john', 't4', 'http://google.com', 1, 5, 0, 0),

ユーザー名「john」の「view_count」の値を合計する必要があるため、出力は「9」になります。

私はこれを試しました

<?php
$dpl = $db->query("SELECT SUM(`views_count`) FROM site_links WHERE username='john'",true);

echo $dpl;
?>

しかし、正しく機能せず、このエラー「リ​​ソースID#92」が発生しました

どうすればそれができますか?

ありがとう

4

1 に答える 1

3

mysqliなどを使用していると思います

<?php

$dpl = $db->query("SELECT SUM(`views_count`) as `total` FROM site_links WHERE username='john'");

$row = $dpl->fetch_assoc();

echo $row['total'];

?>
于 2013-02-27T23:45:28.820 に答える