gdfhghjdfghgfhf Posted April 21, 2008 Share Posted April 21, 2008 Hello, i am using mysql_fetch_array to display a basic query then i echo each one of the entry found i would like to count how many DIFFERENT entries are found with mysql_num_rows it will just count the total entries found Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 What do you mean? Do you want to compare two arrays? O_O Or do you want the number of rows returned by mysql...elaborate please. Quote Link to comment Share on other sites More sharing options...
gdfhghjdfghgfhf Posted April 21, 2008 Author Share Posted April 21, 2008 $cres = mysql_query($cquery) or die(mysql_error()); while($row = mysql_fetch_array($cres)) { $cfields = $row["customfields"]; echo $cfields; } this code will return a list like this cat dog horse cat cat fish I want to count the number of DIFFERENT items, in this array (in example it would be 4 different items) mysql_num_rows would return 6 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 $cres = mysql_query($cquery) or die(mysql_error()); while($row = mysql_fetch_array($cres)) { $cfields = $row["customfields"]; echo $cfields; } this code will return a list like this cat dog horse cat cat fish I want to count the number of DIFFERENT items, in this array (in example it would be 4 different items) mysql_num_rows would return 6 $unique_elements = count(array_unique($array)); It leaves the original array untouched, mind you. Quote Link to comment Share on other sites More sharing options...
gdfhghjdfghgfhf Posted April 21, 2008 Author Share Posted April 21, 2008 i always get "1" as result ... am i supposed to replace $array by $cres or something? Quote Link to comment Share on other sites More sharing options...
Styles2304 Posted April 21, 2008 Share Posted April 21, 2008 you'll replayed $array with whatever the name of your array is. Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 21, 2008 Share Posted April 21, 2008 Have you tried using SELECT DISTINCT ... for your query? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Have you tried using SELECT DISTINCT ... for your query? I've actually never heard of that. But he might need to use the rows for something, but he also wants to count the unique elements. He still needs them returned by the database, probably. So my solution works fine. xD Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 21, 2008 Share Posted April 21, 2008 So my solution works fine. xD Or less than 'fine' with a very large database Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 So my solution works fine. xD Or less than 'fine' with a very large database But what if the unique section that he's looking for has other information on the row? He might need the information. P.S: @Thread starter: Add a unique key to something that REALLY has to be unique next time. Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 21, 2008 Share Posted April 21, 2008 But what if the unique section that he's looking for has other information on the row? He might need the information. Perhaps we should re-read the original question? Then again, scope creep is nothing new in these forums. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 But what if the unique section that he's looking for has other information on the row? He might need the information. Perhaps we should re-read the original question? Then again, scope creep is nothing new in these forums. His first post is ambiguous. You have NO idea what he's actually doing with the data other than "then i echo each one of the entry found". We have two different opinions on coding, so leave it at that. Holy crap. =/ Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 21, 2008 Share Posted April 21, 2008 problem with your solution Darkwater is that no array is actually generated... if including distinct in the query produces the desired result and permits any other interaction required then that would be the best solution. If however the rest of the data set is required for other jobs then creating an array of results would be necessary... (I use mysql_fetch_assoc - for the simple reason that I rarely find returning fields by a numerical index very useful..) $cres = mysql_query($cquery) or die(mysql_error()); while($row = mysql_fetch_assoc($cres)) { $cfields[] = $row["customfields"]; echo $row["customfields"]; } $unique_records = count(array_unique($cfields)); yes this is what darkwater said but the array $cfields was never actually created.... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 problem with your solution Darkwater is that no array is actually generated... if including distinct in the query produces the desired result and permits any other interaction required then that would be the best solution. If however the rest of the data set is required for other jobs then creating an array of results would be necessary... (I use mysql_fetch_assoc - for the simple reason that I rarely find returning fields by a numerical index very useful..) $cres = mysql_query($cquery) or die(mysql_error()); while($row = mysql_fetch_assoc($cres)) { $cfields[] = $row["customfields"]; echo $row["customfields"]; } $unique_records = count(array_unique($cfields)); yes this is what darkwater said but the array $cfields was never actually created.... He just wants the number of unique items. If he wanted the array, he could just use: $newarray = array_unique($oldarray); $count = count($newarray); But that's not what he asked for. =) He wanted an equivalent to mysql_num_rows. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 21, 2008 Share Posted April 21, 2008 He just wants the number of unique items. If he wanted the array, he could just use: $newarray = array_unique($oldarray); $count = count($newarray); But that's not what he asked for. =) He wanted an equivalent to mysql_num_rows. NO he wanted the number of UNIQUE records NOT just the mysql_num_rows.... So tell us - how can he get the number of unique items in the result if he is NOT going to use DISTINCT and doesn't create an array so that it can actually be interrogated for unique values???? PLEASE look at the code I posted once more - you'll see its correct.... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 He just wants the number of unique items. If he wanted the array, he could just use: $newarray = array_unique($oldarray); $count = count($newarray); But that's not what he asked for. =) He wanted an equivalent to mysql_num_rows. NO he wanted the number of UNIQUE records NOT just the mysql_num_rows.... So tell us - how can he get the number of unique items in the result if he is NOT going to use DISTINCT and doesn't create an array so that it can actually be interrogated for unique values???? PLEASE look at the code I posted once more - you'll see its correct.... That's exactly what I did...You don't realize that the count(array_unique()) bit doesn't overwrite the array. He can still use it in a loop after he gets the number of unique records BECAUSE it doesn't overwrite it. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 21, 2008 Share Posted April 21, 2008 Jesus H Darkness... I did acknowledge your correct stipulation in you code - what your FAILED TO DO was generate an array of the database results to run array_unique on... If you look at my code you may spot '[]' in there somewhere - the giveaway of creating an array... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Jesus H Darkness... I did acknowledge your correct stipulation in you code - what your FAILED TO DO was generate an array of the database results to run array_unique on... If you look at my code you may spot '[]' in there somewhere - the giveaway of creating an array... Oh. I thought he already had his array. My bad. I figured he'd know how to make an array, lol. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.