MYSQL/PHP 选择不同

发布于 2024-12-04 20:48:53 字数 1329 浏览 7 评论 0原文

我在另一个页面上调用了这个函数。

function adminnav (){
    $pagewcoms = mysql_query("SELECT DISTINCT pageid FROM comments") or die(mysql_error());
    $idnavrow = mysql_fetch_row($pagewcoms);
    while ($itest = mysql_fetch_row($pagewcoms)) {
        echo "$itest[0] <br />";
    }
}

adminnav('');

表是。

CREATE TABLE `comments` (
  `commentid` int(5) NOT NULL auto_increment,
  `pageid` int(5) NOT NULL default '0',
  `name` text NOT NULL,
  `email` text NOT NULL,
  `comment` text NOT NULL,
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`commentid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=481 ;

INSERT INTO `comments` VALUES(480, 1, '3', '3', '3', '2011-09-13 22:43:06');
INSERT INTO `comments` VALUES(479, 1, '2', '2', '2', '2011-09-13 22:43:01');
INSERT INTO `comments` VALUES(476, 1, '1', '1', '1', '2011-09-13 17:22:49');
INSERT INTO `comments` VALUES(477, 2, 'taylordcraig', '[email protected]', 'this is page two', '2011-09-13 17:26:09');
INSERT INTO `comments` VALUES(478, 3, 'this is page3', 'this is page3', 'this is page3', '2011-09-13 22:28:59');

它只拉出两个结果。 “2 3”[我之前问过这个问题,但措辞不好。]

I have this function called on another page.

function adminnav (){
    $pagewcoms = mysql_query("SELECT DISTINCT pageid FROM comments") or die(mysql_error());
    $idnavrow = mysql_fetch_row($pagewcoms);
    while ($itest = mysql_fetch_row($pagewcoms)) {
        echo "$itest[0] <br />";
    }
}

adminnav('');

The table is.

CREATE TABLE `comments` (
  `commentid` int(5) NOT NULL auto_increment,
  `pageid` int(5) NOT NULL default '0',
  `name` text NOT NULL,
  `email` text NOT NULL,
  `comment` text NOT NULL,
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`commentid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=481 ;

INSERT INTO `comments` VALUES(480, 1, '3', '3', '3', '2011-09-13 22:43:06');
INSERT INTO `comments` VALUES(479, 1, '2', '2', '2', '2011-09-13 22:43:01');
INSERT INTO `comments` VALUES(476, 1, '1', '1', '1', '2011-09-13 17:22:49');
INSERT INTO `comments` VALUES(477, 2, 'taylordcraig', '[email protected]', 'this is page two', '2011-09-13 17:26:09');
INSERT INTO `comments` VALUES(478, 3, 'this is page3', 'this is page3', 'this is page3', '2011-09-13 22:28:59');

It's only pulling two results. "2 3" [I asked this before but worded it poorly.]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-12-11 20:48:53

看起来该查询实际上按其应有的方式提取了 3 个结果。你只是让其中一个离开:

function adminnav (){
    $pagewcoms = mysql_query(...);

    // HERE YOU FETCH ONE ROW BUT DO NOTHING WITH IT
    $idnavrow = mysql_fetch_row($pagewcoms);

    while ($itest = mysql_fetch_row($pagewcoms)) {
        echo "$itest[0] <br />";
    }
}

如果你只是删除那一行,或者稍微调整一下,它应该会很好地显示所有内容:

function adminnav (){
    $pagewcoms = mysql_query(...);
    $idnavrow = null;

    while ($itest = mysql_fetch_row($pagewcoms)) {
        if (empty($idnavrow)) {
            $idnavrow = $itest;
        }
        echo "$itest[0] <br />";
    }
}

Looks like the query is actually pulling 3 results as it should. You are just letting one of them go:

function adminnav (){
    $pagewcoms = mysql_query(...);

    // HERE YOU FETCH ONE ROW BUT DO NOTHING WITH IT
    $idnavrow = mysql_fetch_row($pagewcoms);

    while ($itest = mysql_fetch_row($pagewcoms)) {
        echo "$itest[0] <br />";
    }
}

If you just remove that line, or tweak it a little, it should display everything just fine:

function adminnav (){
    $pagewcoms = mysql_query(...);
    $idnavrow = null;

    while ($itest = mysql_fetch_row($pagewcoms)) {
        if (empty($idnavrow)) {
            $idnavrow = $itest;
        }
        echo "$itest[0] <br />";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文