PHP If statement with two timestamps [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to run a PHP if statement that says if there are more than 3600 seconds between two MySQL timestamps than do something. I think I have the IF statement written correctly but something is going on where it is always being resolved to true.
$time1=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table");
$time2=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table and time <>(SELECT max(time) FROM table)");
if (($time1->fetch_object()->time) - ($time2->fetch_object()->time) > 3600);
{
echo $test
}
When I run:
echo $time1->fetch_object()->time-$time2->fetch_object()->time;
It shows the correct value that I would expect but even if its less than 3600 it still does the echo $test. Any ideas what can be causing this?
Thank you!!!

Use CASE statements, a better and efficient approach:
Select event, case when TIMESTAMPDIFF(SECOND, last_timstamp, first_timestamp)<3600 then `under_time` when TIMESTAMPDIFF(last_timstamp, first_timestamp)>3600 then `over_time` end case as `duration_flag` from your_table where your_conditions
Then in your PHP you condition the output based on the queried flag.
This way you make less requests to the server, and has a cleaner query.
Maybe mysql has an old bug with TIMESTAMPDIFF, so if it doesn`t work, use this instead:
Time_To_Sec(last_timstamp) - Time_To_Sec(first_timestamp)

Related

What does " if(true) " means? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm building a functionality which displays a message dynamically, i.e if the forms are correctly filled or not.
I came across this piece of code and I don't understand how does it really work.
if(true) {
$this->flash('Yay ! File uploaded successfully', 'success');
} else {
$this->flash('There is some error', 'error');
From what I've already looked for, I guess it's used to see whether or not there is an array available, since PHP returns false if there isn't one.
(according to PHP manual )
I still don't get how does this work, there is no variables such as if($foo) but just plain boolean on this condition.
And yes, it seems to work as intended.
Thanks for your enlightments.
This looks like a debugging statement left in.
if(true)
This is always true, so you could remove the test entirely and just run with the true statement.

variable conditions from mysql in switch case on php/mysql? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
my problem is that i have to implement variable conditions which means that the user is able to define a VALUE for period of time.This data will be stored in mysql. PHP is used on server-side.
i.e.
FROM 2018-01-01 TO 2018-01-03 VALUE 10;
FROM 2018-01-04 TO 2018-01-06 VALUE 20;
DEFAULT (used if not in time gap) VALUE 100;
Therefore I thought a switch case can be an option. But is it possible to have a foreach loop on cases?
like:
$date = "2018-01-01";
switch ($date) {
foreach(... as $data){
case $data:
//load variable
break;
}
default:
//load default values
}
Maybe i'm on the wrong way - pls hlp.
No, your syntax is wrong. In all programming languages that I can think of, you are not allowed to overlap blocks. A switch block can be contained in a loop, or one or more of it's cases' blocks may contain their own loops. (switch is a special case, in that may not have code outside it's case statements.)
You're (in a sense) trying to terminate a for-loop block, inside the block of an if statement within it.
The problem with your code, is that it will be unmaintainable, difficult to understand, and slightly inefficient.
The easiest thing to do, would be to run a cron job or something else which would be periodical and check if today's date falls in the date region defined by your user in the mysql data.
You could simply do with a better SQL statement to grab the data and store it in a slightly different way - for example:
start_date
end_date
and then your SQL result would do all the work of your current switch statement (the switch statement won't work as expected anyway).

mysql: how to select with string containing "-" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Could someone please point me where did I go wrong?
I've an apps using PHP and mysql. The thing is, we are using some file uploader plugins that require us to save the unique id that has format something like this a41ddc78-3fee-4bf3-88c1-83028ae22f1. At some point, I need to select data from the database using this fields and as you can see, I can't get the field when I do manual query on it. any help is appreciate on how to solve this problem. Thank you
SELECT * FROM product_image WHERE unique_id = 'a41ddc78-3fee-4bf3-88c1-83028ae22f1'
the query works fine...

PHP comparing two POST not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm currently having an issue with PHP, I'm trying to compare two POST variables using an if statement. However when i use != it will not work, but == will work and i don't understand why.
if($_POST["PasswordSignUp"] != $_POST[PasswordSignUpRepeat]){
setcookie("PassMatch","Error");
}
Edit:
Okay, i just had an idea that may fix my problem, at the moment i am using the cookie as basically a global variable. Is there a way to define a global variable to use from page to page without having to 'include' or 'require' it into the code.
$_POST[PasswordSignUpRepeat] should be $_POST['PasswordSignUpRepeat']. Also, use paranthesis in comparison. It isn't mandatory, but it's a good process to follow.
if(($_POST["PasswordSignUp"]) != ($_POST['PasswordSignUpRepeat'])){
setcookie("PassMatch","Error");
}
Try below:-
if($_POST['PasswordSignUp'] !== $_POST['PasswordSignUpRepeat']){
setcookie("PassMatch","Error");
}
!== will check fr value and type.

Query error after OR in MySQL and PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have problem with query to base, problem appears after OR:
Parse error: syntax error, unexpected 'or' (T_LOGICAL_OR) in
Here is the code:
$chat2=mysql_query('SELECT * from chat where
user1!="" OR user2!="" AND status="oczekuje" AND user1="'.$_SERVER['REMOTE_ADDR'].'"
OR user2="'.$_SERVER['REMOTE_ADDR'].'" '); or die();
$dodaj2=mysql_fetch_array($chat2);
$ilosc2=mysql_num_rows($chat2);
Here screen from sublime: http://scr.hu/47nh/haujn
Why "OR" is not on red color like AND?
Right now, or die(); is a separate statement because there is a ; right before it. You should change to
$chat2=mysql_query('SELECT * from chat where
user1!="" OR user2!="" AND status="oczekuje" AND user1="'.$_SERVER['REMOTE_ADDR'].'"
OR user2="'.$_SERVER['REMOTE_ADDR'].'" ') or die();
Furthermore, you want to consider:
use mysqli_* or PDO to prevent SQL injection attacks
add some parentheses to the query. How can you know that MySQL evaluates AND and OR in the same way as intended?
To elaborate point 2, AND takes precedence over OR, so it is evaluated as
user1!="" OR
(user2!="" AND status="oczekuje" AND user1="'.$_SERVER['REMOTE_ADDR'].'") OR
user2="'.$_SERVER['REMOTE_ADDR'].'"

Categories