Duplicate elements in a mutidimensional array PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have been trying, but I cannot figure this myself...
I have a MySQL table that contains info like this (plus an ID column):
Artist Song Uploader
art1 song1 name1
art2 song2 name2
art1 song1 name3
I would like to use php to present this table with this output:
Artist Song Times uploaded
art1 song1 2
art2 song2 1
The main idea is to find the duplicates (artist + song) inside my table.
I have tried using array_count_values() with the variable that contains all the data from the MySQL table. But this outputs this error:
Warning: array_count_values(): Can only count STRING and INTEGER values! in index.php on line 40
I'd really appreciate some help.
Thanks all!

Don't do this in PHP, do it in your database query
SELECT artist,
song,
count(id) as uploadCount
FROM mytable
GROUP BY artist,
song
Let the database do what databases are good at doing

Related

How do I limit same search results on my auto suggesting program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a table named Pets that has a column of owner_name. The user wants to put the owner's name and it will suggest the possible owner names kinda like if I put Jo on the search box, it suggests John,Jonathan,Joshua,Jorge. But the problem is, there are times when an owner has multiple columns under their name with different pets. Kinda like John owns a cat,dog,bird. My program does the search suggestion but the problem is that it repeats the names, so if I typed Joh it lists three Johns on the auto suggest. Out of curiosity I tried the LIMIT 1 but as expected, what it did was limited the entire results to 1 so if I put Jo, it just displays John.
Use the DISTINCT keyword. Try something like:
SELECT DISTINCT columnname
FROM query
WHERE ....

Display all data from DB to php page [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to display all data that are related to each other.
For example, I have a sales_item table
itemid | srfno | qty | serial | description
1 |1234 |1 |354 |laptop
2 |1234 |2 |456 |iphone
I want to display the 2 qty, serial and description since they both belong to the same srfno. I can only output the first one. Pls help me.
my select statement to count all the same srfnos
Select COUNT(sales_item.srfno) as srfno from sales_item;
getting data from db:
$exqty=$row['exqty'];
$exserial=$row['exserial'];
$exdesc=$row['exdesc'];
I output it using:
<input name=qty value="<?php echo $exqty ?>"/>
if you issue the following select statement:
Select COUNT(sales_item.srfno) as srfno from sales_item;
you will get a row array in php like this:
Array (
0 => Array (
"srfno" => 2
)
)
Since the result does not contain any member with the name exqty the statement $row['exqty']; will end in an error undefined index "exqty".
Either you adjust your Select statement ("select * from sales_item") or your php code - depends on what you're trying to achieve.
Besides:
Select COUNT(sales_item.srfno) as srfno from sales_item;
will run into problems as your expression COUNT(sales_item.srfno) is named the same as the column itself. Better:
Select COUNT(sales_item.srfno) as srfno_count from sales_item;

Counting the number of different values in a MySQL column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm partially familiar with the COUNT function, but I'm a little stumped as to how I can apply it to this specific situation. Suppose I have a table with one of the columns containing the following values:
apples
apples
apples
oranges
oranges
bananas
These values can be added by users, so I never know what values may end up in this column, but there will always be duplicates. What I first want to do is to display them in a HTML table, so that each row will represent one particular value, and each adjacent cell shows the number of duplicate values. Using the example data above, the expected output in a HTML table generated by PHP would be:
|Fruits | Amount |
|-----------|----------|
|apples | 3 |
|oranges | 2 |
|bananas | 1 |
At this stage, I know that I have to use the COUNT function in SQL, and through PHP, I need to run some sort of foreach loop, but beyond that, I'm not sure how to proceed.
EDIT: Changed the data. Hopefully it makes a little more sense
SELECT [column_name], COUNT([column_name])
FROM [table_name]
GROUP BY [column_name];
Add GROUP BY valueName; to the end of your query.
Select `column name`, count(*) from `table1`
group by `column name`

MySQL SELECT * from 2 tables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a very dumb question.
Lets assume that I have 2 tables, A which has 10 rows and B which has 100 rows.
I know that when I enter SELECT * FROM A, B, then the result would be 1000 rows.
My question is, why?
Can someone please explain this to me?
Because this is a cross join, which gives you a cartesian product between the two tables.
Wikipedia :
CROSS JOIN returns the Cartesian product of rows from tables in the
join. In other words, it will produce rows which combine each row from
the first table with each row from the second table
So in this case, 10 * 100 = (guess what) 1000.
If you want to see 110 resord in the result you should use UNION to combine results from two tables:
(SELECT * from A)
UNION
(SELECT * from B)
You have to integrate a relation with some WHERE, for edit this output

mysql find record by field/column value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hy everyone.
I working an app for android which is query result from database. the android side is OK, i can transfer data in and out from DB server to android. but i have difficulties in PHP and mysql stuff, specifically to find record by field/column value and print the row that matched the value i search.
Let's see this is my database structure :
database name is LBS, table name is information.
ID, NAME, JOB
1, john, teacher
2, doe, farmer
3, obama, farmer
4, sweden, teacher
what syntax i can use to print row that have for example teacher value form JOB column ?, so the output is like this :
1, john, teacher
4, sweden,teacher
Thanks in advance ! :)
Just make a where condition
SELECT * FROM information WHERE job = 'teacher';
Then you will get output as
ID |NAME |JOB
---|-------|------
1 |john |teacher
4 |sweden |teacher
SELECT * FROM information WHERE JOB = 'teacher'
Try for outputting as string:
SELECT CONCAT_WS(',', ID, NAME, JOB) WHERE JOB='teacher'

Categories