0

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', '[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');

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 only 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?]

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't that be $itest[0]? mysql_fetch_row returns an enumerated array. mysql_fetch_array would get the associative array. Right?
I reasked my question but worded it better. I knew the [0] it just only pulls two. [ i reasked stackoverflow.com/questions/7412157/mysql-php-select-distinct ]
1

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 />';
    }

Comments

0

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.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.