How do I make mysql_num_rows of multiple results

6

Well, what I want is the following:

I have 1 table designated by items, in this table I have 4 columns:

  • name
  • iditem (Unique ID)
  • numerobot

What I want to do is: I want to make a mysql_num_rows of all results, but for each different item name.

In other words, let's suppose that I have 10 results with the name "Shadow Case", 15 results with the name "Gamma Case".

What I want is to do this, so that you know how much of each item I have.

To finish I want to get these values, and make an update to another table that is called infoitens. This table only has 2 columns which are name and quantity, where in quantity I want to put the previous result, for each item.

I hope you have understood me.

    
asked by anonymous 15.02.2017 / 01:12

2 answers

4

It can be done right in a query in MySQL:

SELECT nomeitem, count(nomeitem) as quantidade FROM tabela GROUP BY nomeitem;

With this you have the quantity of each item.

You can get the result in a MySQLi_Result, or inside can directly update to another table:

INSERT INTO outra_tabela (nomeitem, quantidade) 
SELECT nomeitem, count(nomeitem) as quantidade FROM tabela GROUP BY nomeitem;
15.02.2017 / 01:25
3

mysql_num_rows will not resolve easily.

You can make a normal query that returns the total of each item:

SELECT nomeitem, COUNT(iditem) as total FROM tabela GROUP BY nomeitem;

This query will return something like this:

nomeitem       |total
----------------------
Shadow Case    |10 
Gamma Case     |15

Otherwise, mysql_* functions are deprecated, preferably mysqli_* or PDO class.

link link

    
15.02.2017 / 01:29