I have several activities in my database, but I would like to hide the ones from the past, so I modified my MySQL request:
$sql = "SELECT * FROM tblAgenda WHERE date <= CURDATE() order by date ASC";
But it doesn't do a thing except giving errors. What's wrong?
It seems like you're not getting any results, so that is throwing errors. You always need to check for results before looping, you cannot just assume that every query will return something.
Also, if you want things from the present/future, your comparison operand is backward:
$sql = "SELECT * FROM tblAgenda WHERE date >= CURDATE() order by date ASC";
Putting it together:
$sql = "SELECT * FROM tblAgenda WHERE date >= CURDATE() order by date ASC";
$result = mysql_query($sql);
if (!$result) {
// do something to handle zero results here
} else {
// do your usual while... loop
while ($row = mysql_fetch_assoc($result)) {
// code for each result row
}
}
Related
SELECT * FROM time_slot JOIN reseve_slot ON reserv_start_time WHERE reserv_date = '09-07-2015' AND (time_slot NOT BETWEEN reserv_start_time AND reserv_end_time)
Hard to go on what you asked but it seems you want the query results as they are minus the very last returned row.
Here's a verbose example to show the removal clearly:
$query = "select * from time_slot join reseve_slot on reserv_start_time where reserv_date = '09-07-2015' and (time_slot NOT between reserv_start_time and reserv_end_time)";
$result = mysqli_query($con, $query) or die($mysqli->error);
$resultArray = mysqli_fetch_assoc($result);
if (!is_null($resultArray)) {
//remove the last result
$wantedResult = array_pop($resultArray);
// now do something with the returned results $wantedResult
}
$id = 2;
// query to fetch delayed
$sql = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
$obResult = $objConnection->query($sql);
// query to fetch if there is no delayed
$q = "SELECT * FROM leads WHERE status = ('$id') AND later != '1' ORDER BY postnummer LIMIT 1";
$oResult = $objConnection->query($q);
// primary query to run, which makes use of the two above accordingly
$query = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
$objResult = $objConnection->query($query);
while ($row = $objResult->fetch_object()) {
if (new DateTime() > $row->delay && $row->delay != '0000-00-00 00:00:00') {
while ($row = $obResult->fetch_object()) {
echo $row->firmanavn;
}
} else {
while ($row = $oResult->fetch_object()) {
echo $row->firmanavn;
}
}
}
Changed my code to this, and still i got the same problem, the if clause if met, but it echoes from my else instead
Reason is that you are using same variable names that overwrite each other. $objResult rename this to something like $objResult2 for the inner query.
One thing to keep in mind is that your inner query inside a loop is really unnecessary unless you did not provide some piece of code. You can just put that query outside of while loop. Will save you time & memory.
Although I think there are other ways this code could be cleaned up, it seems to me that you're attempting to compare a DateTime object to a date string, which is going to yield unpredictable results. Try changing your while part to:
$current_date = new DateTime();
while ($row = $objResult->fetch_object()) {
if (
$current_date->format('Y-m-d H:i:s') > $row->delay &&
$row->delay != '0000-00-00 00:00:00'
) {
while ($row = $obResult->fetch_object()) {
echo $row->firmanavn;
}
} else {
while ($row = $oResult->fetch_object()) {
echo $row->firmanavn;
}
}
}
I'm not sure this will fully solve your issues, but it should get you closer.
I want to query if there is a particular timestamp for a day using the distinct dates that are present in a table and so far I have come up with this code
$mysqli = new mysqli('localhost', 'root', 'rolemodel', 'dashboard');
$dates = "SELECT DISTINCT DATE_FORMAT(mytimestamp, '%Y-%m-%d') FROM ".$this->table_name." ORDER BY DATE(mytimestamp) ASC;";
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$rowsdata = $mysqli->query($dates); // get number of rows
$num_rows_date = $rowsdata->num_rows;
for ($i = 0; $i<=$num_rows_date; i++) {
$current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE mytimestamp =".$rowsdata[$i]; //not sure on what to use here
}
I am confused on how to query the exact timestamp which is supposed to be the particular day along with the 00:00:00
Example 2014-06-02 00:00:00
Hope my question makes sense.
try:
//$rowsdata = $mysqli->query($dates); // get number of rows
//$num_rows_date = $rowsdata->num_rows;
//for ($i = 0; $i<=$num_rows_date; i++) {
//This is usually the way you want to do this with plain old arrays
if($result = $mysqli->query($dates)){
while($row = $result->fetch_row()){
$current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE DATE(mytimestamp) ='".$row[0] . "'";
//do something awesome here ...
}
}
See the docs for the DATE function.
BUT ... it looks like you're just querying the same table you pulled the dates from in the first place, so you're guaranteed to find timestamps with those dates. Not sure what you're ultimately after, but you may be able to do it with a single query.
I am looking for a way to count the number of times a result occurred for team X and a number of times a result occurred for team Y.
Example
I have a table containing a sports competition results from 2009 - present.
A user can query the DB to retrieve all results when selected 2 teams played against each other.
The results gets displayed in tables as follows:
Now I am looking for a way to calculate the total number of games played between the two selected teams, and how many time the result went in favour of (in this example) Stormers, and how many times the result went to the Sharks
Is it possible to do this without changing data in my table table? Ive been trying to come up with some sort of logic to solve the above problem but I am stumped.
My code to retrieve data follows:
if($venue == "hometeam"){
$result= " SELECT *
FROM `results`
WHERE `hometeam` = '$team1' && `awayteam` = '$team2'" or die(mysql_error());
}
else if($venue == "awayteam"){
$result = " SELECT *
FROM `results`
WHERE `awayteam` = '$team1' && `hometeam` = '$team2'"or die(mysql_error());
}
else if($venue =="all"){
$result = " SELECT *
FROM `results`
WHERE (`hometeam` = '$team1' AND `awayteam` = '$team1') OR (`hometeam` = '$team2' AND `awayteam` = '$team2')"or die(mysql_error());
}
My code to display the data:
cho '<td>'.$row['gamedate'].'</td>';
echo '<td>'.$row['hometeam'].'</td>';
echo'<td>'.$row['awayteam'].'</td>';
echo'<td>'.$row['homescore'].'</td>';
echo'<td>'.$row['awayscore'].'</td>';
if($row['homescore'] > $row['awayscore']){
echo'<td style="background-color:yellow">'.$row['hometeam'].'</td>';
}
else echo'<td>'.$row['awayteam'].'</td>';
Any help or advice will be greatly appreciated. Thanks in advance
$rs = mysqli_query($link, "select * from results where result = \"Stormers\"");
$stormers = mysqli_num_rows($rs);
$rs = mysqli_query($link, "select * from results where result = \"Sharks\"");
$sharks = mysqli_num_rows($rs);
Now, variable $stormers will show the number of results matching Stormers, and Sharks will match results with Sharks. You can also use the Count() MySQL function instead of select, but I was assuming you might want to get more data from it instead of just a number later on.
How about
SELECT `result`, COUNT(*) FROM `results` WHERE `hometeam` IN (`$team1`,`$team2`) AND `awayteam` IN (`$team1`,`$team2`) GROUP BY `result`
I'm assuming you're iterating through the results to gen the table rows. Why not just count the wins up while you do that?
$homeTeamWins = 0;
$awayTeamWins = 0;
$ties = 0;
while( $row = $result->fetch_object() ) {
if( $row->homescore > $row->awayscore ) {
$homeTeamWins++;
} elseif( $row->homescore < $row->awayscore ) {
$awayTeamWins++;
} else {
$ties++;
}
// output the html rows
}
I have searched and searched for ways to do this but have found very limited information.
I have a MySQL table 'msgdb' that contains a field 'ttime' that is in the format double(25,8) (example row = 1352899856.95249200).
I need to routinely cleanup the table by removing any rows where the field 'ttime' is <= today's date -5 days.
These are the only two lines of code I could find related to double to time conversion but cannot get either to work.
SELECT ADDDATE(ADDDATE(ADDDATE('1899-12-31 00:00:00',FLOOR(ttime)), INTERVAL -1 DAY),INTERVAL(MOD(ttime,1)*86400)SECOND) AS TrueDate FROM msgdb
select date('1899-12-31 00:00:00'+ INTERVAL ttime * 24*3600 SECOND) as date from msgdb
I have tried first to display any rows that match the criteria using the code below, before I started using DELETE FROM to make sure I'm getting the correct results.
$query = "select date('1899-12-31 00:00:00'+ INTERVAL ttime * 24*3600 SECOND) as date from msgdb";
$result = mysql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo $row['date'];
echo '<br>';
}
and also
$query = "SELECT ADDDATE(ADDDATE(ADDDATE('1899-12-31 00:00:00',FLOOR(ttime)), INTERVAL -1 DAY),INTERVAL(MOD(ttime,1)*86400)SECOND) AS TrueDate FROM msgdb";
$result = mysql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo $row['TrueDate'];
echo '<br>';
}
but both are returning nothing.
UPDATE: Ok so by using this code:
$query = "select ttime from msgdb";
$result = mysql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo date('m-j-Y, H:i:s', $row[0]);
echo '<br>';
}
I am able to see it convert 'ttime' field from the stored value of 1352899856.95249200 to 11-14-2012, 07:30:56.
So how would I DELETE from the table all rows where ttime is <=now - 5 days?
Figuring out which records have a date before a point in time should be easy:
DELETE FROM table WHERE ttime <= DATE_SUB(NOW(), INTERVAL 5 DAY);
It might also be better to use UTC_TIMESTAMP() if you store all your times in UTC, which is the only sane way to do it.