Here is my table register:
id name student_id date presence absence_note Tusername
50 hassimkhan 1112815 2014-03-08 P saahir
56 Karishma kods 1119112 2014-03-08 P saahir
58 Karishma kods 1119112 2014-03-09 P saahir
60 hassimkhan 1112815 2014-03-09 A saahir
Here is my loop to display the result:
$retrieve = mysql_query("SELECT DISTINCT(student_id),presence FROM register");
while($row = mysql_fetch_array($retrieve))
{
echo $row['student_id'];
}
I know for e.g id 56 and 58 with student_id: 1112815 record are different in a way, but i want to display it only one time e.g of result of these data:
1112815
1119112
Any help?
Try GROUPing instead:
SELECT student_id, presence FROM register GROUP BY student_id
(untested)
retrieve = mysql_query("SELECT student_id,presence FROM register GROUP BY student_id");
while($row = mysql_fetch_array($retrieve))
{
echo $row['student_id'];
}
Related
I have 3 different tables
table 1 keep general scores over the year
table 2 and 3 is event specific
only common field in all 3 is a member ID
I need to select 5 top score results for each member from table1 combine them with 1 specific result from table 2 and 3
I managed to get everything together in a temp table, but cnt get the output the way I need it
example what i have and need.
Original code
$prevQuery = "SELECT distinct member_id FROM scores";
$prevResult = $conn->query($prevQuery);
while($row = $prevResult->fetch_assoc()) {
$scores=("SELECT member_id,event_id,event_date,event_score FROM scores where member_id = ".$id." ORDER BY event_score DESC LIMIT 5");
Query:
select * from temp_table order by mem_id asc
PHP:
you can simple do your expected result in your application like this
<?php
$result = array(array('mem_id'=>1,'location'=>'A','date'=>'20/05/2017','score'=>100),array('mem_id'=>1,'location'=>'B','date'=>'21/05/2017','score'=>103),array('mem_id'=>1,'location'=>'C','date'=>'22/05/2017','score'=>106),array('mem_id'=>1,'location'=>'C','date'=>'23/05/2017','score'=>108),
array('mem_id'=>2,'location'=>'A','date'=>'20/05/2017','score'=>105),array('mem_id'=>2,'location'=>'B','date'=>'21/05/2017','score'=>109),array('mem_id'=>2,'location'=>'C','date'=>'22/05/2017','score'=>111),array('mem_id'=>2,'location'=>'C','date'=>'23/05/2017','score'=>110));
$new_result=array();
foreach($result as $key=>$row)
{
$new_result[$row['mem_id']][]=$row;
}
echo "<table border='1px'>";
echo "<thead><tr><th>S.No</th><th>date</th><th>score1</th><th>date</th><th>score2</th><th>date</th><th>score3</th><th>date</th><th>score4</th></tr>";
echo "<tbody>";
foreach($new_result as $key=>$row)
{
echo "<tr><td>".$key."</td>";
foreach($row as $key1=>$row1)
{
echo "<td>".$row1['date']."</td>";
echo "<td>".$row1['score']."</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
OUTPUT :
S.No date score1 date score2 date score3 date score4
1 20/05/2017 100 21/05/2017 103 22/05/2017 106 23/05/2017 108
2 20/05/2017 105 21/05/2017 109 22/05/2017 111 23/05/2017 110
I have these two tables in my database :
table_A: table_B:
id user id user
1 Mike 1 Mike
2 Dan 2 Dan
3 Tom 3 Tom
4 Lina 4 Lina
5 Cynthia
6 Sam
And i'm using this SQL query, to detect which users in table_B do not exist in table_A, and select those users (not existing in table A) ids:
SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
And i'm using this php script to put the ids in a variable $not:
$sql=" SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A)";
$result = mysqli_query($con,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row = mysqli_fetch_array($result)){
$not=$row[0];
}
Now if i want to extract 2 elements with my sql query:
SELECT table_B.id, table_B.name FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
How can i place both, each in a variable using the above script, so i'll be able to insert each element in a different table in the database?
You can put the values in a multidimensional array.
$not = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$not[] = array(
"id" => $row['id'],
"name" => $row['name']
);
}
echo $not[0]['id']; //Returns 5
echo $not[0]['name']; //Returns Cynthia
echo $not[1]['id']; //Returns 6
echo $not[1]['name']; //Returns Sam
in your query define table_B.id as user_id and table_B.name as user
then in your php use mysqli_fetch_object like this
while($row = mysqli_fetch_object($result)){
$not= $row;
//or if you want to store all the results use $not[] = $row; and define $not as array
}
PHP and MYSQLi
I am trying to get the below output: for a Project ID (PJ006)
I put the "date" column in Descending order first and then i want to skip all the Repeated value of the first value (Skip all entries of EM207) and display the immedate 2nd value (Output expected: EM206). This output is needed against the for a Project ID (PJ006)
Database:
SL No. Badge ID Date ProjectID
1 EM207 2015-05-12 00:00:00 PJ006
2 EM207 2015-05-11 00:00:00 PJ006
3 EM207 2015-05-09 00:00:00 PJ006
4 EM207 2015-05-09 00:00:00 PJ001
5 EM206 2015-05-08 00:00:00 PJ001
6 EM206 2015-09-05 00:00:00 PJ006
7 EM206 2015-09-03 00:00:00 PJ006
8 EM205 2015-09-01 00:00:00 PJ006
Expected output: For Project ID: PJ006,
Badge ID: EM206
PHP Code for: Using the First Badge ID (EM207), fetch the data of the Project ID PJ006 and display
$mysqli=mysqli_connect('serverIP','username','password','databasename');
//This data i passed by Ajax
$someID = trim($_POST["storedValue"]);
//This query puts the date column in Desending Order
$query2 ="SELECT * FROM scores1 WHERE ProjectID='$someID' order by ScoreDate desc limit 1";
$result = mysqli_query($mysqli,$query2)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result))
{
//the output is displayed here
echo $row[1];
}
What modification is required in the above code "to skip all the Repeated value of the first value (Skip all entries of EM207) and display the immediate 2nd value (Output expected: EM206)". This output is needed against the for a Project ID (PJ006)
$query2 ="SELECT * FROM scores1 WHERE ProjectID='$someID' order by ScoreDate desc limit 1";
$result = mysqli_query($mysqli,$query2)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
$badgeid=0; $proid=0;
while($row= $database->fetch_array($result))
{
if($badgeid!=$row['batchid'] || $proid!=$row['proid']; ){
echo "$badgeid and $proid "
$b=$row['batchid'];
$p=$row['proid'];
}
}
In this answer i am thanking Mr.Thamaraiselvam for helping me with the exact required output. Thanks You.
The Solution which worked:
$mysqli=mysqli_connect('serverIP','username','password','databasename');
$query2 ="SELECT * FROM scores1 where ProjectID='12345' ORDER BY ScoreDate DESC";
$result = mysqli_query($mysqli,$query2)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
$badgeid=0; $proid=0 ; $date=0;
$flag=0;
while($row=mysqli_fetch_array($result))
{
if($badgeid!=$row['BadgeID'] || $proid!=$row['ProjectID'] ){
if($flag==2){
echo "$badgeid and $date<br>";
}
$flag++;
$badgeid =$row['BadgeID'];
$proid =$row['ProjectID'];
$date =$row['ScoreDate'];
}
}
?>
Okay, as discussed, what you're actually after is the most recent badge id for a particular project - the fact that this is the second entry is a bit of a red herring and may not hold true in other scenarios.
I'd use a subquery to find the most recent entries for each project:
select
badgeId,
projectId
from
scores1,
(
select
max(date) as max,
projectId
from
scores1
group by
projectId
) as sub
where
scores1.date = sub.max
and scores1.projectId = sub.projectId
You can then add a check for whichever project id you're interested in. Don't forget to add some indexes for better performance.
I have the following MySQL table:
table_1
id | value_1 | value_2 | value_3
1 hehe haha stack
2 over flow me
3 123 abc hello
4 hi random php
5 html js css
How can I select value_2 from 3rd row, which is "abc"?
Something like $rows[3]['value_2']
Here's the code I had tried with:
$sql=mysql_query("SELECT * FROM table_1 ORDER BY `table_1`.`id` ASC");
$rows=mysql_fetch_array($sql);
//And then I tried accessing it by "$rows[3]['value_2']" and $rows['value_2'][3]
I can't use SELECT * FROM table_1 WHERE id='3', because I need to access multiple lines (all of them). I can't use a WHILE loop also, because I need the values in very different places in the code.
mysql_fetch_array only returns a row corresponding to the query. The proper use of mysql_fetch_array is as such:
$sql=mysql_query("SELECT * FROM table_1 ORDER BY `table_1`.`id` ASC");
while ($row = mysql_fetch_array($sql)) {
printf("ID: %s value_2: %s", $row[0], $row[2]);
}
http://www.php.net/manual/en/function.mysql-fetch-array.php
Found the answer. You can now access them how you wanted.
$sql=mysql_query("SELECT * FROM settings ORDER BY `settings`.`id` ASC");
$id=0;
while ($rows = mysql_fetch_array($sql)) {
$settings[$id]['value_1']=$rows['value_1'];
$settings[$id]['value_2']=$rows['value_2'];
$settings[$id]['value_3']=$rows['value_3'];
$id++;
}
echo $settings[5]['value_3'];
echo $settings[5]['value_1'];
echo $settings[4]['value_2'];
echo $settings[0]['value_3'];
Can someone help me with this:
I have a mySQL database and I would like to do a simple search amoung the items, here is an example of my database named "orders"
id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
How can I group by duplicated data and display it like this:
<select name="costumer">
<option>Hansen</option>
<option>Nilsen</option>
<option>Jensen</option>
</select>
I know it is the GROUP BY command, but dont know how to display it on a PHP script file
Try this:
$dbconn = new mysqli();
$dbconn->connect("localhost","root","","test");
if($dbconn->connect_errno ){
echo "Connection Failed";
}
$query = "SELECT DISTINCT Customer FROM `Orders`";
$result = $dbconn->query($query);
echo "<select name=\"costumer\">";
while($row = $result->fetch_array())
{
echo "<option>".$row["Customer"]."</option>";
}
echo "</select>";
you can use DISTINCT on this since you only need to have 1 column,
SELECT DISTINCT Customer FROM `Orders`