Display count of unique values in MySQL db table using PHP - php

So I have a table named pins that has 3 columns that need to be taken into consideration. These columns are plan and order_id.
I need to get a count for all of the pins that have an order_id=0 and plan=9.
This is what I have so far:
$qT="SELECT plan, COUNT(*) as cnt FROM pins WHERE order_id=0 and plan=9";
$res=mysql_query($qT);<br/>
mysql_free_result($res);
while($row = mysql_fetch_array($res)) {<br/>
echo $row['plan'];<br/>
}
Any help in displaying the results would be a great help.

Try to group your pin using GROUP BY, So
$result = mysql_query("SELECT plan, COUNT(pin) as cnt FROM pins WHERE (order_id=0 and plan=9) GROUP BY plan");
echo "<table border='1' style='border-collapse: collapse'>";
echo "<th>Plan</th><th>Count</th>";
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>".$row['plan']."</td><td>".$row['cnt']."</td></tr>";
}
echo "</table>";
mysql_free_result($result);
?>

Related

Numbering the array rows

I have selected the whole table with mysql, there are records of sold tickets.
When I am writing them out with foreach, they are showing correctly from the newest sold ticket at the top to the oldest purchase at the bottom.
The problem is that the IDs do not look good. I have done many test purchases before launching it and the id numbers are starting from 50 now.
I would like to keep it as it is now but only number the records with normal numbers from 1. The problem is that the highest number must be at the top since the latest record is there on top. Could somebody advice me on how to do this please?
When you use while loop you have to create one variable before start loop and increatement it by 1 in loop and use it as row number :)
$sql ="SELECT * FROM tbl_purchases ORDER BY id DESC";
if ($result = $link->query($sql)) {
echo "<table border='1'>";
$line_counter=mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>".
"<td>{$line_counter}</td/>".
"<td>{$row['id']}</td/>".
"<td>{$row['name']}</td>".
"</tr>";
$line_counter--;
}
echo "</table>";
}
Usually, when you're fetching from DB, the ID column which is usually on auto-increment is not reliable, so when fetching, you should run your own auto-increment. i.e.
<?php foreach($rows as $index => $row): ?>
<table>
<td><?php echo $index + 1 ?></td>
to display with custom index in descending order using php
$sql ="SELECT * FROM tbl_purchases ORDER BY id DESC";
if ($result = $link->query($sql)) {
$index = mysqli_num_rows($result);
echo "<table border='1'>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>".
"<td>{$index}</td/>".
"<td>{$row['id']}</td/>".
"<td>{$row['name']}</td>".
"</tr>";
$index--;
}
echo "</table>";
}
for ascending index
$sql ="SELECT * FROM tbl_purchases ORDER BY id DESC";
if ($result = $link->query($sql)) {
$index = 1;
echo "<table border='1'>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>".
"<td>{$index}</td/>".
"<td>{$row['id']}</td/>".
"<td>{$row['name']}</td>".
"</tr>";
$index++;
}
echo "</table>";
}
==================
To reset the auto incremental column in DB (start again from 1)
For MYISAM
ALTER TABLE tbl_purchases AUTO_INCREMENT = 1;
For INNO DB
SET #num := 0;
UPDATE tbl_purchases SET id = #num := (#num+1);
ALTER TABLE tbl_purchases AUTO_INCREMENT =1;

K/D Ratio with SQL and PHP

So I'm trying to make a K/D ratio with some data from a database using SQL.
The main problem is that the tables are so strange and I'm trying to display it as an HTML table.
There's a table called stats_player_kill (when player kills another)
Here's the content of the table:
So if I want to get the kills, I count the killer column with the same id.
Here's how I get number of kills of each player and display it as an HTML table:
<table class="tablitas" id="kills">
<thead>
<tr>
<th data-column-id="name">Player</th>
<th data-column-id="killer" data-type="numeric">Kills</th>
</tr>
</thead>
<?php
$sql = "select p.name as name, count(*) as amountOfObjects
from stats_player_kill b
join stats_player p on p.id = b.victim
group by b.victim
order by count(*) desc
limit 15";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td>".$row["amountOfObjects"]."</td></tr>";
}
echo "</tbody>";
echo "</table>";
echo "</td>";
} else {
echo "0 results";
}
?>
So I need help making the math to divide number of kills by the total deaths.
Sorry for my English and thanks to everyone! :D

Multiple PHP/MYSQL WHERE statements, same action for each

Trying to get PHP to read my MYSQL db for transactions, divide them by "category" and print the total for each category.
Using a similar structure as the code below, how can I make it do all of the below for each category number. In my db, categories are a number 1-10.
Any help would be much appreciated!
<?php
$result = mysql_query("SELECT SUM(amount) AS value_sum FROM TRANSACTIONS where category=2")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo "<div class='col1'>";
echo round ($row['value_sum'], 2);
echo "</div>";
}
?>
$result = mysql_query("SELECT SUM(amount) AS value_sum
FROM TRANSACTIONS
GROUP BY category");
Now you have a row per category.

2 while loops in php - mysql select statement

I have 2 tables in DB:
clients (Show all clients data)
clientsmany (admin could add many phone numbers for each client)
I would like to print all the details about the clients in 1 html table and if any client has more than phone number, all the numbers are printed in the same cell of 'td'
<?php
$result = mysql_query("SELECT * FROM clients");
$result1 = mysql_query("SELECT clientsmany.phone, clients.ID FROM clients INNER JOIN clientsmany ON clients.ID=clientsmany.ClientID");
while($row = mysql_fetch_array($result)){ //Not read!
while($row1 = mysql_fetch_array($result1)){ //Working correctly and show the list of 'phone' in $row1 for clientsmany.phone
echo "<center><table border='1'>";
echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."<br>".$row1['phone']."</td></tr>";
echo "</table></center>";
}}
?>
Why the 1st while is not working??
The 2nd while only works and print a correct data then it exit automatic!
<?php
echo "<center><table border='1'>";
$result = mysql_query("SELECT * FROM clients");
while($row = mysql_fetch_array($result)){
$result1 = mysql_query("SELECT * FROM clientsmany WHERE clientsid=".$row['id']);
if(mysql_num_rows($result1)>0 ){
while($row1 = mysql_fetch_array($result1)){
echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."<br>".$row1['phone']."</td> </tr>";
}
}else{
echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."</td></tr>";
}
}
echo "</table></center>";
?>
Use GROUP_CONCAT to create a single query and you will be able to a single loop
GROUP_CONCAT will take a column that is repeated and separate each value with a comma (by default it can be changed) and return it into a single value
$query = <<<END
SELECT
clients.*,
GROUP_CONCAT(clientsmany.phone) as phonenums
FROM
clients
INNER JOIN
clientsmany ON clients.ID=clientsmany.ClientID
GROUP BY
clients.ID
END;
A query like this would give you all the clients table columns and a column named phonenums which will be a comma separated list of the phone numbers
Now since you only have one query you only need one loop
$db = mysqli_connect(...);
...
//only need to echo out the <table> part once
//so taken out of the while loop
echo "<center><table border='1'>";
$result = mysqli_query($db,$query);
while( ($row = mysqli_fetch_assoc($result)) ) {
echo <<<END
<tr>
<td>{$row['ID']}</td>
<td>{$row['SomeOtherColumn']}</td>
<td>{$row['phonenums']}</td>
</tr>
END;
}
//Again the </table> only needs done once
//so taken out of the loop
echo "</table></center>";
Notice the mysli_* functions being used. Mysql api is depreciated, for the most part you can just rename the functions you currently use to mysqli_, but note that some require the $db link as a parameter so make sure to read the php manual on each of the functions so you know how to call them correctly.
Also note that I am using heredoc syntax instead of doing multiple echo calls
First, mysql_* functions are depreciated. Try to use mysqli_* functions.
If you want to display data in 1 html table, so why you have started while above table tag?
Try this query..
SELECT clientsmany.phone, clients.* FROM clients, clientsmany WHERE clients.ID=clientsmany.ClientID"
Then use while statement below table tag (No need of 2 different while loop)...
echo "<center><table border='1'>";
while($row1 = mysqli_fetch_array($result1)) {
// your <tr><td> code
}
echo "</table></center>";

Displaying items from multiple tables on your webpage using php while loop

I am trying to dsiplay data from 2 different tables from my mysql database using a while loop.
Currently I can display the data from 1 table and limit the results to 3. I then want to display the first 5 records from another table. If I join the tables I can only display the same number of items from both using LIMIT?
I am using a while loop to display the content from a table called item, using the following code;
$query");
$result2 = #mysql_query($query, $connection)
or die ("Unable to perform query$query");
<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php
}
?>
If I start another loop for the data from the next table called movie, however the data is not displayed using the following code;
<?php
while($row= mysql_fetch_array($result2))
{
?>
<?php echo $row['title'] ?>
<?php
}
?>
What is the best way to display the data from the 2 tables?
Many Thanks
I don't know if you forgot to paste a bit of code, but this should work:
<?php
$query = "select * from item order by date, time asc limit 0,3";
$result = mysql_query($query);
while($row= mysql_fetch_array($result))
{
echo $row['item'];
}
$query2 = "select * from movie limit 0,5";
$result2 = mysql_query($query2);
while($row= mysql_fetch_array($result2))
{
echo $row['movie'];
}
?>
You may be able to do it with one SQL Query too:
SELECT i.item, m.movie
FROM (SELECT * FROM item ORDER BY date, time ASC LIMIT 0,3) i,
(SELECT * FROM movie limit 0,5) m
Then in php:
<?php
while($row= mysql_fetch_array($result))
{
echo $row['item'];
echo $row['movie'];
}
?>
But that depends on how you want to format the output.

Categories