开发者

MYSQL/PHP SELECT DISTINCT

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 looks like this

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', 'taylordcraig@gmail.com', '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');

My problem is the function prints "array array" I can deal with the data, but shouldn't there be 3 distinct results? This is the closest I've been. The idea is to search the table and find pages with comments, which I will later list as links.

So the result I'm looking for would be

"1 2 3"

thank you in advance.

EDIT:

I'm sorry I worded my question poorly. The porlem isnt "array array" it's that there's on开发者_JS百科ly two. I can make it display but it doesnt matter. I still only have "2 3" displaying, as said before, I only had two array data.

[** me, am I going to have to re-ask this now?]


Should be echo "$itest[0] <br />";


Check the PHP docs: http://php.net/manual/en/function.mysql-fetch-row.php

mysql_fetch_row — Get a result row as an enumerated array

You're not fetching a value, you're fetching an array. The reason you only see "array array" is because the first result is getting "disappeared" on the $idnavrow = mysql_fetch_row($pagewcoms); line.

Change your function to the following and I'm sure you'll see all 3 numbers you expect:

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


mysql_fetch_row returns a an array containing all of the items in the row. Instead of printing $itest, you should print $itest[0].

You could also use mysql_fetch_assoc and print $itest['pageid'] for clarity.


mysql_fetch_row() returns an array, even if it only contains one element. Your "echo" should look like this:

echo "{$itest[0]} <br />";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜