honkmaster Posted October 23, 2011 Share Posted October 23, 2011 Hi, can anyone help me with this. I have a column in my table call "machine", I want to count the number of time each appears and return the results into an array In the example below the number of times each printer appears in the column is returned to the array ____________ machine ____________ fb7500-1 fb7500-2 turbojet turbojet xl1500-1 xl1500-2 canon roland When I run the following I get the names as well as the values and I just want the values?? Result like this Canon 1 FB7500-1 1 Roland 1 TurboJet 2 Vutek QS3200 1 XL1500-1 1 <?php mysql_connect("localhost","XX","XX"); @mysql_select_db("schedule") or die( "Unable to select database"); date_default_timezone_set('Europe/London'); $query = "SELECT machine, COUNT(machine) FROM maindata GROUP BY machine"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ $array = array( 'key1' => $row[0], 'key2' => $row[1],'key3' => $row[2],'key4' => $row[3], 'key5' => $row[4],'key6' => $row[5],'key7' => $row[6]); extract($array); echo "$key1<br>$key2<br>$key3<br>$key4<br>$key5<br>$key6<br>$key7"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249643-help-with-array-and-count/ Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2011 Share Posted October 23, 2011 Why are you echoing more than the number of fields returned by the query? They query returns 2 fields, and you're echoing 7. If you don't want a result, either don't select it at all, or don't echo it. Quote Link to comment https://forums.phpfreaks.com/topic/249643-help-with-array-and-count/#findComment-1281541 Share on other sites More sharing options...
honkmaster Posted October 23, 2011 Author Share Posted October 23, 2011 What I'm trying to do rather badly I feel is echo back just the count part and not the name of the printer. So I'm getting this Canon 1 and I want 1 I want to return the result into some think like this $key1 Cheers Chris Quote Link to comment https://forums.phpfreaks.com/topic/249643-help-with-array-and-count/#findComment-1281542 Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2011 Share Posted October 23, 2011 You're returning 2 fields in each result, and then for whatever reason, you move those fields from the array they're already in to another array along with 5 more values that don't even exist. Then you extract all those values fro the second array and echo them. That logic makes no sense at all. Just return the field you need, which is COUNT(machine), GROUPed BY machine, and echo only that one field, if that's all you need. You should also have the following directives set in your php.ini file while developing: display_errors = On and error_reporting = -1 so you're aware of any errors generated by your code. <?php $query = "SELECT COUNT(machine) FROM maindata GROUP BY machine"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "$row[0]<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/249643-help-with-array-and-count/#findComment-1281551 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.