$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";
$post = $db->select($query);
?>
<?php
if ($post) {
while ($result = $post->fetch_assoc()) {
$var = "select * from cse_routine where day = '".$result['day'] ."'";
print $result['day']."<br>";
if ($results=$db->select($var))
{
while ($rows = mysqli_fetch_assoc($results))
{
print $rows['t_slot']. " " . $rows['room']. " " . $rows['day']. " " . $rows['c_code'];
echo "<br>";
}
}
echo "<br>";
}
}
echo "<br>";
?>
Running this code; I found this:
It's ok. But actually, I want output that not print the same date repeatedly. See here Saturday, Sunday print 3 each of them but I want that Saturday print 1 and Sunday print for 1.
You just change you query to
$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section'
GROUP by 'day'";
You could try using DISTINCT constraint (http://www.mysqltutorial.org/mysql-distinct.aspx) in first SQL:
$query="SELECT DISTINCT day FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";
So you will have Saturday and Sunday only once each.
Related
I am trying to find if two runners have ever ran in the same race. The two runners are Peter Smith and Diane Peters.
$resultRaceType = mysqli_query($db,"SELECT DISTINCT date,time FROM results where runner = 'Peter, Smith' ");
while($row = mysqli_fetch_array( $resultRaceType ))
{
$resultRaceType1 = mysqli_query($db,"SELECT * FROM results where date = ' " . $row['date'] . " ' and time = ' " . $row['time'] . " ' and runner = 'Diane, Peters'");
while($row1 = mysqli_fetch_array( $resultRaceType1 ))
{
echo "<tr >";
echo "<td>";
echo $row1['date'];
echo " - " . $row1['time'];
echo "</td>";
echo "<tr>";
}
}
The above code works, but only if I limit the first select to LIMIT 50. So I can see that it is timing out. My table has over 100K rows. I know I am doing something wrong but cant see what it is.
Thanks for any help you guy's can give me.
Try:
SELECT a.date, a.time FROM results a
JOIN results b ON (a.date = b.date AND a.time = b.time)
WHERE a.runner='Peter, Smith' AND b.runner='Diane, Peters';
<?php
//db connection goes here
$arr=array('12:30:00','01:30:01','02:30:01','03:30:01','04:30:01','05:30:01','06:30:01','07:30:01');
$arr1=array('01:30:00','02:30:00','03:30:00','04:30:00','05:30:00','06:30:00','07:30:00','08:30:00');
$cnt=count($arr);
for($i=0;$i<$cnt;$i++){
$sql="SELECT count(*) FROM report WHERE DATE_FORMAT(dt,'%H:%m:%i') BETWEEN $arr[$i] AND $arr1[$i]";
}
//fetching in while and echo
this is my script that i am trying which will generate report counting number of user and number of application from two different table report and report1.
the report will be like
Time count Logged In user Count-Apps
12:30:00-01:30:00
01:30:01-02:30:00
02:30:01 -03:30:00
03:30:01-04:30:00
04:30:01-05:30:00
05:30:01-06:30:00
06:30:01-07:30:00
07:30:01-08:30:00
08:30:01-09:30:00
report table for counting number of user
user datetimeuser(datetime)
a 12:30:00
b 01:30:00
c 01:30:01
d 02:30:00
report1 table for counting number of apps
user datetimeuser(datetime)
a 12:30:00
b 01:30:00
c 01:30:01
d 02:30:00
previously i have done a script which does the work but its slowing the server as my script will be placed in cron job firing in 1 hour interval and fetching the result
previous.php
$time_ranges = array(
array('12:30:00','01:30:00'),
array('01:30:01', '02:30:00'),
array('02:30:01', '03:30:00'),
array('03:30:01', '04:30:00'),
array('04:30:01', '05:30:00'),
array('05:30:01', '06:30:00'),
array('06:30:01', '07:30:00'),
array('07:30:01', '08:30:00'),
array('08:30:01', '09:30:00'),
);
$sql="SELECT sub0.TimeRange, sub0.number, COUNT(*) AS countapps
FROM
(
SELECT
CASE
";
foreach ($time_ranges as $r) {
$sql .= "
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '$r[0]' and '$r[1]'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '$r[0]'), '%Y-%m-%d %H:%i:%s') ";
}
$sql .= "
ELSE NULL
END AS StartRange,
CASE ";
foreach ($time_ranges as $r) {
$sql .= "
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '$r[0]' and '$r[1]'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '$r[1]'), '%Y-%m-%d %H:%i:%s') ";
}
$sql .= "
ELSE NULL
END AS EndRange,
CASE ";
foreach ($time_ranges as $r) {
$sql .= "
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '$r[0]' and '$r[1]'
THEN '$r[0]-$r[1]' ";
}
$sql .= "
ELSE NULL
END AS TimeRange,
COUNT(*) as number
FROM report
WHERE DATE_FORMAT(dt,'%Y:%m:%d')=DATE(CURDATE())
GROUP BY StartRange, EndRange, TimeRange
HAVING TimeRange IS NOT NULL
) sub0
LEFT OUTER JOIN report1
ON report1.dt BETWEEN sub0.StartRange AND sub0.EndRange
GROUP BY sub0.TimeRange, sub0.number";
$query=mysql_query($sql);
echo'<html>
<head>
<title>Count User Info TimeWise</title>
</head>
<h1>Count User</h1>
<table border="3" cellspacing="2">
<tr>
<th>range</th>
<th>count</th>
<th>Apps Count</th>';
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['TimeRange'] . "</td>";
echo "<td>" . $row['number'] . "</td>";
echo "<td>" . $row['countapps'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</html>";
?>
i want to make the mysql query shorter and more precise by taking only two array and looping it.but could not really make it.please help.how can i do this taking two array and then counting(array) and for loop it and count statement in mysql
$arr=array('12:30:00','01:30:01','02:30:01','03:30:01','04:30:01','05:30:01','06:30:01','07:30:01');
$arr1=array('01:30:00','02:30:00','03:30:00','04:30:00','05:30:00','06:30:00','07:30:00','08:30:00');
$cnt=count($arr);
for($i=0;$i<$cnt;$i++){
$sql="SELECT count(*) AS test FROM report WHERE DATE_FORMAT(dt,'%H:%m:%i') BETWEEN $arr[$i] AND $arr1[$i]";
Possibly dynamically build up a select to return the times and then join that against you report table:-
$numbers = array();
foreach($arr AS $key=>$value)
{
$numbers[] = "SELECT '".$arr[$key]."' AS StartRange, '".$arr1[$key]."' AS EndRange ";
}
$dates_select = "(".implode(" UNION ",$numbers).") sub0";
$sql="SELECT sub0.StartRange, sub0.EndRange, count(report.dt)
FROM $dates_select
LEFT OUTER JOIN report
ON DATE_FORMAT(report.dt,'%H:%m:%i') BETWEEN sub0.StartRange AND sub0.EndRange
GROUP BY sub0.StartRange, sub0.EndRange";
I have had a long road to get to this last question. Everything is my code is working now, but I can't get this last little issue. Right now I have:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$val = $row["value_sum"];
$plan = $row["currentplan"];
$remain = $plan - $val;
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
It only subtracts the first value as opposed to the values for all. displayed like this:
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td>".$remain . " MB</td> ";
echo "<td>".$row['email'] . "</td></tr>";
}
So my goal is to subtract all value_sums from all dataplans, but what I have now, gives me the first value for all columns. Thank you!
mysql_fetch_assoc() will always get one row. You can use it in loop, or better use PDO, eg. like this:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$results = $pdo->query($sql);
You can read about creating PDO connections here http://www.php.net/manual/en/book.pdo.php
Im trying to display the total for each project group. The Problem is that all the values for that field are recorded like so:
0h 06m
The sum is displaying as:
0
with the following code. How do i display the total result in proper HH:MM format
<?
$query = "SELECT taskname, SUM(tasktime) FROM tictoc WHERE uid = '6' GROUP BY taskname";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)) {
echo "<br />";
echo "Total ". $row['taskname']. " = <strong>". $row['SUM(tasktime)']."</strong>";
echo "<br />";
}
?>
If tasktime is a timestamp data-type, then you can use DATE_FORMAT(tasktime,'%Hh %im').
How can I make those dates line up with start date and end date or selectively echo the dates rather than have the while loop echo them?
For example if I can echo "Start Date" then the actual date then the "end date" and then the date.
Basically how may I echo the dates from mySQL selectively rather than using the loop to echo them?
for example if I want to echo row 2 can I do echo $row'date' (to echo the 2nd row)
On HTML Page PHP CODE:
Start Date:
End Date:
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<li>".$row['date']."</li>";
echo "<br />";
}
?>
It sounds like to you need to constrain your query results. If you only want certain records from your database then you should filter out the ones you don't want right in the SQL.
$query = "SELECT * FROM Date1 WHERE date >= ? AND date <= ?";
If you use prepared statements you can plug in values for the two questions marks.
first solution
$arr = Array();
while($row = mysql_fetch_array($result)){
$arr[] = $row;
}
echo $arr[0]['date']; // first
echo $arr[3]['date'];
echo $arr[7]['date'];
echo $arr[count($arr)-1]['date']; // last
second solution
$first = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date ASC LIMIT1"));
$last = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date DESC LIMIT1"));
echo $first['date'].'<br>';
echo $last['date'];
if you want to use second solution remember to put index on date column
also...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT min(date) as startdate, max(date) as enddate FROM Date1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row) {
echo "Start Date: " . $row['startdate'] . "<br />\n";
echo "End Date: " . $row['enddate'] . "<br />\n";
}
?>
???
err...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
echo "<table border=\"0\">\n<tr><th>Start Date</th><th>End Date</th></tr>";
$startRow = true;
while($row = mysql_fetch_array($result)) {
if($startRow) {
echo "\n<tr><td>";
} else {
echo "<td>";
}
echo $row['date'];
if($startRow) {
echo "</td>";
} else {
echo "</td></tr>";
}
$startRow = !$startRow;
}
if(!$startRow) {
echo "</tr>";
}
echo "\n</table>\n";
?>
???