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>";
Related
The main page I am using is called the Project Details Page which when you select a project number on the form will query the subform for any records pertaining to that project number and display them in this page named the (TasksSubform).
This page called (TasksSubform) uses a php file called mysqli_connect.php to obtain a database connection and assigns that connection to $dbc in the mysqli_connect.php file.
This page then query’s table 1 named 'CommonTasks', and starts displaying the data row by row in a table on the page using
while($row = $result->fetch_assoc())
Currently one of the columns in the record being displayed is named “AssignedTo” which produces the unique ID number in the Employees table instead of the text value of the employees name associated with the ID number. So, I need to be able to list the records in the CommonTasks Table using Fetch then, when it tries to display the value in the “AssignedTo” column within the Common Tasks Table, it must lookup the ID in the Employees table which equals the same value in the Common Tasks Table, and replace the number value of the Assigned To Field with the text value in the Employees table.
COMMONTASKS
EMPLOYEES
* Add
* AssignedTo
* Attachments
* Cost
* CostInDays
* Description
* DueDate
* EmployeeID
* ID
* PercentComplete
* Priority
* StartDate
* SubmissionDate
* Title * ID
* Address
* BusinessPhone
* City
* Company
* CountryRegion
* EmailAddress
* FaxNumber
* FirstName
* HomePhone
* JobTitle
* LastName
* MobilePhone
* Notes
* StateProvince
* WebPage
* ZIPPostal Code
This is what I have. Yet, all it is producing is a blank in the Assigned To field on the php page.
enter image description here
I am a novice to php and mysql.
This is probably something simple which I am overlooking.
Yet, I have been troubleshooting various methods for the past few days, and just cant seem to figure out what I am doing wrong.
<?php
// Get a connection for the database
require_once('../mysqli_connect.php');
// Create a query for the database
$sql = "SELECT * FROM `CommonTasks`";
$employee = "SELECT ID, LastName, LastName FROM Employees JOIN CommonTasks ON Employees.ID=CommonTasks.AssignedTo";
$emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = CommonTasks.AssignedTo LIMIT 1";
$emp1 = "SELECT id as LastName, FirstName FROM Employees WHERE ID = CommonTasks.AssignedTo LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$result1 = #mysqli_query($dbc, $sql);
$result2 = #mysqli_query($dbc, $emp);
$result = $dbc->query($sql);
$link = "commntasks-insertdata.php"
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Common Tasks-subform</title>
<meta name="viewport"charset="utf-8" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
echo " <table border='1' #6a8fba>
<caption>SUBFORM - Common Tasks</caption>
<tr>
<th>Job Title</th>
<th>Due Date</th>
<th>Start Date</th>
<th>Cost</th>
<th>Priority</th>
<th>Percent Complete</th>
<th>Assigned To</th>
<th>Description</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><a href= $link > $row[Title] </a></td>";
echo "<td>". $row['DueDate'] . "</td>";
echo "<td>". $row['StartDate'] . "</td> " ;
echo "<td>". $row['Cost'] . "</td>";
echo "<td>". $row['Priority'] . "</td>";
echo "<td>". $row['PercentComplete'] . "</td> " ;
echo "<td>". $row ['SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = $_GET[AssignedTo] LIMIT 1'] . "</td>";
echo "<td>". $row['Description'] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";
?>
</body>
Currently, the results are being produced by this line
$result = $dbc->query($sql);
The following line will not execute a mysql query.
echo "<td>". $row ['SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = $_GET[AssignedTo] LIMIT 1'] . "</td>";
As written, you are trying to find a row value in $result that does not exist. You need to call the second query within the first while loop and pass the value of $_GET[AssignedTo] which is probably $row[AssignedTo]
Something like this
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><a href= $link > $row[Title] </a></td>";
echo "<td>". $row['DueDate'] . "</td>";
echo "<td>". $row['StartDate'] . "</td> " ;
echo "<td>". $row['Cost'] . "</td>";
echo "<td>". $row['Priority'] . "</td>";
echo "<td>". $row['PercentComplete'] . "</td> " ;
$emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = '$row[AssignedTo]' LIMIT 1";
$result2 = #mysqli_query($dbc, $emp);
$row2 = $result2->fetch_assoc();
echo "<td>". $row2 ['Firstname'] . " ". $row2 ['Lastname'] . "</td>";
echo "<td>". $row['Description'] . "</td> " ;
echo "</tr>";
}
So, Here are my revisions to your example:
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><a href= $link > $row[Title] </a></td>";
echo "<td>". $row['DueDate'] . "</td>";
echo "<td>". $row['StartDate'] . "</td> " ;
echo "<td>". $row['Cost'] . "</td>";
echo "<td>". $row['Priority'] . "</td>";
echo "<td>". $row['PercentComplete'] . "</td> " ;
$emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = '$row[AssignedTo]'";
$result2 = #mysqli_query($dbc, $emp);
$row2 = $result2->fetch_assoc();
echo "<td>". $row2['LastName']," , ",$row2[FirstName] . "</td>";
echo "<td>". $row['Description'] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";
?>
Which produces this: Results of modified code
Thank you so much for your Help!
Since I am converting an Access Database to mySQL and recreating all of the queries, forms, and reports.... I am sure I will have an overwhelming amount of questions in the near future.
Eric
I have 3 tables i want to display the 3 table data in single table based on primary key, foreign key the result came perfectly! But i need to calculate rank based on the total marks from my second table.
result screenshot:
Please anyone tell me the query to calculate rank
I used the following mysql query
if(isset($_POST['submit']))
{
$result = mysqli_query($con,"
SELECT s.student_name
, s.contact_number
, m.total
, m.rank
, p.father_name
FROM student_details s
JOIN mark m
ON s.student_id = m.student_id
JOIN parents_details p
ON p.student_id = s.student_id
WHERE s.student_name = '".$_POST['student_name']."'
");
echo "<table border='1' align='center' cellpadding='15' bgcolor='#FFFFFF'>
<tr>
<th>NAME</th>
<th>CONTACT NUMBER</th>
<th>TOTAL MARK</th>
<th>RANK</th>
<th>FATHER NAME</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['student_name'] . "</td>";
echo "<td>" . $row['contact_number'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['father_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
}?>
SELECT * FROM
(
SELECT #rank := #rank+1 finalrank,ZZ.* FROM
(
SELECT student_details.student_name,
student_details.contact_number, mark.total,
mark.rank, parents_details.father_name
FROM student_details
INNER JOIN mark ON student_details.student_id=mark.student_id
INNER JOIN parents_details ON parents_details.student_id=student_details.student_id ,(SELECT #rank:=0)z
ORDER BY mark.total desc
)ZZ
)ZZZ
WHERE ZZZ.student_name = '".$_POST['student_name']."'
Just try above query.
Here I had used SELECT #rank:=0 and #rank := #rank+1.
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 an trying to display records in table with top 10 enteries with specific color
My Query just for Ref.
$sqlsum=mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC LIMIT 10");
This code below displays a simple table, I need to display table With TOP 10 entries be different in colour[blue].. rest remains the same [white background].
i.e Top 10 can be in blue color, and rest in white color.
Below is the code I am using to display records.
<?php
while($row = mysql_fetch_array($sqlsum))
{
echo "<tr> ";
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
}
echo "</tr> " ;
?>
I have this table structure as sample. That I want to use, with this code. The table need to be the same , but i am not to find the logic , how to build the table
with this structure
<table>
<thead><tr><td colspan="2"><center>Prizes</center></td></tr><tr>
<th>Position</center></th><th><center>Prize</center></th></tr></thead>
<tbody><tr><td>1st</td><td>0.0$</td></tr>
<tr class="alt"><td>2nd</td><td>0.0$</td></tr>
<tr><td>3rd</td><td>0.0$</td></tr>
<tr class="alt"><td>4th</td><td>0.0$</td></tr>
<tr><td>5th</td><td>0.0$</td></tr>
</tbody>
</table>
Remove Limit 10 For Fetch All Data:
$sqlsum=mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC");
php:
<?php
$i=1;
while($row = mysql_fetch_array($sqlsum))
{
echo "<tr ".(($i <= 10) ? "bgcolor='blue'" : '')'."> ";
// Apply attrinute bgcolor for backgroung color
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr>";
$i++;
}
?>
Set an indicator variable to point it.
<?php
$rowNumber = 0;
while($row = mysql_fetch_array($sqlsum))
{
if ($rowNumber < 10)
{
echo "<tr class=\"alt\"> ";
}
else
{
echo "<tr> ";
}
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
$rowNumber++;
}
?>
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