Jump to content

Recommended Posts

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";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/249643-help-with-array-and-count/
Share on other sites

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.

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>";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.