Scip Posted June 29, 2009 Share Posted June 29, 2009 Ok i have a result from a database query which i am using array_count_values to count the number of times a value is repeat in the array. The problem is it returns twice the number of expected values. here is some code: <?php $query = "SELECT "; $query .= "item1, item2, item3, item4, item5, item6, item7, item8, item9, item10 "; $query .= "FROM loot "; $query .= "WHERE "; $query .= "Date < NOW() "; $query .= "AND Date > DATE_ADD(NOW(), INTERVAL -1 MONTH)"; $result = mysql_query($query,$connection); confirm_query($result); $loot = mysql_fetch_array($result); if($loot){ echo count($loot) ."<br/>"; $requests = array_count_values($loot); print_r($requests); }else{ $message = "An error occured with the query."; } ?> each item in the database should only have one value so array_count_values should return a value of one for each of item1 to 10. but it's return a value of two for each. Also an echo of loot should only return a value of 10 since i am only select 10 items from the database but it's 20. here is the output from the above code: 20 Array ( [Ebonite] => 2 [Aluminite] => 2 [Kobalit] => 2 [Dragoturkey Peak] => 2 [bear Bone] => 2 [black Dragoss Horn] => 2 [ice Kwak Beak] => 2 [Dragoturkey] => 2 [barley] => 2 [barley Sugar] => 2 ) I tried commenting everything from "WHERE" onwards from the query statement but i still get the same results. any help is apperiacted. thanks Link to comment https://forums.phpfreaks.com/topic/164089-results-from-db-query-and-array_count_values/ Share on other sites More sharing options...
dzelenika Posted June 29, 2009 Share Posted June 29, 2009 Use MYSQL_ASSOC or MYSQL_NUM third parameter to fetch array values indexed by name or ordinary number. If you don't use them then you get both values Link to comment https://forums.phpfreaks.com/topic/164089-results-from-db-query-and-array_count_values/#findComment-865600 Share on other sites More sharing options...
Scip Posted June 29, 2009 Author Share Posted June 29, 2009 thanks i will give that a try. Edit: Worked!! Link to comment https://forums.phpfreaks.com/topic/164089-results-from-db-query-and-array_count_values/#findComment-865605 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.