I want to count all user records and display them in tables, I am trying this code code, It displays the record for one user only, I want to display records from all users.
$u=$_POST['userid'];
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name where user_id=$u");
echo "<table border='1'>
</tr>
<tr>
<th>User ID</th>
<th>count</th>
</tr>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $u . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Try the following SQL Query:
SELECT `user_id`, COUNT(`user_id`) as `total` FROM `table-name` GROUP BY `user_id`;
Refer to the documentation of the GROUP BY clause.
Use below:
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name");
where clause use for filter the data.
refer http://www.w3schools.com/sql/sql_where.asp
Related
I have code that takes values from a database that belong to a particular user.
<?php
$user = $_SESSION['username'];
$q = intval($_GET['q']);
$db = mysqli_connect('localhost', 'username', 'password', 'database_name');
$sql = "SELECT * FROM savedtimes WHERE username = '$user' AND session = '$q' ORDER BY timeid";
$result = mysqli_query($db, $sql);
//$SNo = I might need to put something over here which makes it show the order number.
echo "<table>
<tr>
<th>S.No.</th>
<th>time</th>
<th>Avg of 5</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $SNo . "</td>";//This is the S.no column.
echo "<td>" . $row['value2'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Now, while it is displaying these values in the order of a field called timeid, which is on auto increment, I want to have a column called S.No (only while displaying, not in the actual database), which orders them from 1 to the last number. Please note that I can't just use 'timeid', because I have multiple users, so the numbers won't be continuous. So basically, the order is that of 'timeid', but I want a column showing up with continuous numbers. How do I do this?
Declare a counter variable (with a value of 1) outside of the while() loop. Display it inside the loop and subsequently increment it at the end of the loop.
// your code
$SNo = 1;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $SNo . "</td>";
echo "<td>" . $row['value2'] . "</td>";
echo "</tr>";
++$SNo;
}
// your code
I am trying to get all values shown in a table from mysql but getting one .
I want to get the rows of the mysql table at in the last table mentioned in the code
////////Here is a desc of no use --- blah for just posting this question / as i am getting an error msg for giving more details information about this question /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the code:
$sql = 'SELECT
item_added
FROM
products_added
ORDER BY id';
$results = mysqli_query($conn, $sql);
if(mysqli_num_rows($results) < 1){
echo "No items";
}else{
$new_sql = 'SELECT
item_added,
quantity,
amount,
sum(amount) as items_total
FROM
products_added
where `username` = "'.mysqli_real_escape_string($conn, $_SERVER["REMOTE_ADDR"]).'"
ORDER BY id';
$resu = mysqli_query($conn, $new_sql);
}
?>
<table>
<thead>
<tr>
<td>Item</td>
<td>Qyt</td>
<td>Price</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($resu)){
echo "<tr>";
echo "<td>" . $row['item_added'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td><a class=\"remove-from-cart\" href=\"\"><i class=\"fa fa-times\"></i></a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
It looks like its because you're using an aggregate function SUM() without a GROUP BY. In the $new_sql query, try adding "GROUP BY item_added" right before "ORDER BY id".
I would like to ask how to show one row only in same id, in below example, I just want to show the highest score result in the table.
Here is my code
$query = "SELECT * FROM memberdata ORDER BY score DESC";
echo "<table border=1>
<tr>
<th> id </th>
<th> score </th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "</tr>";
}
echo "</table>";
And the Output is
id score
1 5000
1 4000
1 3000
1 500
2 3000
2 1000
Use Group by
SELECT id, MAX(score) AS score FROM memberdata GROUP BY id
Try this:
SELECT id, MAX(score) AS score FROM memberdata GROUP BY id
or this:
SELECT * FROM memberdata ORDER BY score DESC LIMIT 1
You need to iterate to find the maxrow first.
$query = "SELECT * FROM memberdata ORDER BY score DESC";
echo "<table border=1>
<tr>
<th> id </th>
<th> score </th>
</tr>";
$maxrow = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)) {
if ($maxrow['score'] > row['score']) maxrow = row;
}
echo "<tr>";
echo "<td>" . $maxrow['id'] . "</td>";
echo "<td>" . $maxrow['score'] . "</td>";
echo "</tr>";
echo "</table>";
I am trying to display data from multiple rows in sql in a single table box.
in the format of:
"MovieTitle" "movieReleaseYear" "directorName"
The Matrix 1999 Andy Wachowski, Lana Wachowski
where Andy Wachowski and Lana Wachowski comes from different rows but are gathered with group_concat
I have no problem getting them in phpmyadmin but i don't know how to display it in php.
I have this:
$sql="SELECT 725G54_5_movies.MovieTitle, 725G54_5_movies.movieProductionYear, GROUP_CONCAT( 725G54_5_director.directorName )
FROM 725G54_5_movies
JOIN 725G54_5_directed ON 725G54_5_movies.MovieID = 725G54_5_directed.movieID
JOIN 725G54_5_director ON 725G54_5_directed.directorID = 725G54_5_director.directorID
GROUP BY 725G54_5_movies.MovieTitle
ORDER BY $order ASC";
$result=mysql_query($sql);
//Presentation av kontakterna via while-sats till ett formulär
while($rows=mysql_fetch_array($result)){
echo "<tr>
<td>"; echo $rows['MovieTitle']; echo "</td>
<td>"; echo $rows['movieProductionYear']; echo "</td>
<td>";
while($director=mysql_fetch_array($rows['directorName'])){ echo $director; };
echo"</td>
</tr>";
Group Concat has already concatenate the name of the directors, so you don't need to iterate on $rows['directorName'] .This part in your code id wrong because $rows is a row from the query result and $rows['director'] is a value from this row, you can't iterate on a value.
$sql = "SELECT GROUP_CONCAT( 725G54_5_director.directorName ) AS directorNames ..."
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $rows['MovieTitle'] . "</td>";
echo "<td>" . $rows['movieProductionYear'] . "</td>"
echo "<td>" . $rows['directorNames'] . "</td>"
echo"</tr>";
}
You use GROUP BY 725G54_5_movies.MovieTitle so directorNames field will be the result of all directorName concatenated for this Movie.
The crucial bit of information is that you need to alias your GROUP_CONCAT so that you can access this in the array. I would also use a Heredoc when outputting blocks of html for readability:
$sql=
"SELECT
725G54_5_movies.MovieTitle,
725G54_5_movies.movieProductionYear,
GROUP_CONCAT(725G54_5_director.directorName) directorNames
FROM
725G54_5_movies
JOIN
725G54_5_directed
ON 725G54_5_movies.MovieID = 725G54_5_directed.movieID
JOIN
725G54_5_director
ON 725G54_5_directed.directorID = 725G54_5_director.directorID
GROUP BY
725G54_5_movies.MovieTitle
ORDER BY
$order ASC";
$result=mysql_query($sql);
//Presentation av kontakterna via while-sats till ett formulär
echo "<table>";
while($rows=mysql_fetch_array($result)){
echo <<<HTML
<tr>
<td>{$rows['MovieTitle']}</td>
<td>{$rows['movieProductionYear']}</td>
<td>{$rows['directorNames']}</td>
</tr>
HTML;
}
echo "</table>";
I have a database with a table named "photos". In that table i have a column "size"(which is the size of the photos i had upload). I want to get the sum of these sizes and print the sum in my php page. Can anyone give me an example code(sql code in php code)..??
i have try this:
$query = "SELECT SUM(ph_size) from photos";
$result= mysql_query($query,$con);
echo "<table border='1'>
<tr>
<th>SUM(ph_size)</th>
</tr>";
while($row = mysql_fetch_array($result))
{
//echo $row;
echo "<tr>";
echo "<td>" . $row['SUM(ph_size)'] . "</td>";
echo "</tr>";
}
echo "</table>";
but it doesnt work.
You can use the sum function
$result=mysql_query("SELECT SUM(p.size) AS sum_of_photos_sizes FROM `photos` p WHERE 1");
if (mysql_num_rows($result)){
$data=mysql_fetch_array($result);
echo 'Total size of photos: '.$data['sum_of_photos_sizes'];
}