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;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
two columns
user_id purchase_item
1 watch
1 TV
2 watch
2 fridge
2 Toys
when i use distinct comand in sql
same as it is show data but i want to following
user_id:1
purchase_item:watch
tv
user_id:2
purchase_item:watch
fridge
Toys
means i want user id print only one time so how it is possible in php
plz help me and tell coding please
If you want to do it completely in Mysql you can use aggregation to get items as comma separated list per user
select user_id, group_concat(purchase_item) purchase_item
from table
group by user_id
this will give you results like
user_id purchase_item
1 watch,TV
2 watch,fridge,Toys
Also note using group_concat the result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet
But its better if you do this in php just get the ordered results and apply your logic to show user_id only once for multiple purchase_item related to that user
select * from table order by user_id asc
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 5 years ago.
Improve this question
I am looking for some help,
I have two tables, one being users and another being land. users can have many pieces of land.
my users table holds - ID | USERNAME | PASS
The Land table holds - ID | Land_ID |Land_Name
I am trying to create an SQL statement that will allow me to create a new 'land' record but it will populate the 'Land_ID' with the 'ID' from the users table. Therefore:
The user with ID 1 has land name green which has the Land_ID 1 meaning they own that land?
I hope that makes sense.
Thank you in advance for any help.
$var = $session_id; //use whatever variable you use when collecting the session id, from what I gather you are using the unique id from the users table for this.
$sql = "INSERT INTO land (land_id, land_name) VALUES ('$var', '$land_name')"
Extra info:
If this is coming from an HTML form you can input the users session id as a hidden input:
<input type = "hidden" name="id" value="<?php echo['id']; ?>">
In this case you could then define the variable as:
$var = $_POST['id'];
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 ....
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`
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