0
I have a query in my PHP code that was working normal, but suddenly stopped working without me having made any kind of changes.
SELECT * FROM comportamento_loja
where ativado = 1
and datahoje BETWEEN DATE_FORMAT(STR_TO_DATE(data_inicial, '%Y-%m-%d'), '%d/%m/%Y') AND DATE_FORMAT(STR_TO_DATE(data_final, '%Y-%m-%d'), '%d/%m/%Y')
I tried it directly in phpadmin that before was working and also does not work anymore. I made a test with this other query direct in phpadmin:
SELECT * FROM comportamento_loja
where ativado = 1
and 2018-07-05 BETWEEN (data_inicial) AND (data_final)
And it doesn’t work, but if I put quotes in the date it works:
SELECT * FROM comportamento_loja
where ativado = 1
and '2018-07-05' BETWEEN (data_inicial) AND (data_final)
So I tried using this query that works in PHP but also doesn’t work, maybe because the parameter is without quotes, I don’t know. I’ve been doing this for two days and nothing, I’ve tried many ways and nothing, can anyone find a solution to this problem?
I am using: Win7, wamp server, PHP version 7, 5.7.14 - Mysql and also PDO, but as I said all queries I also test in phpadmin and nothing.
This is my code:
$data_atual = date("d/m/Y");
$sql = "SELECT * FROM comportamento_loja where ativado = 1 and ? BETWEEN DATE_FORMAT(STR_TO_DATE(data_inicial, '%Y-%m-%d'), '%d/%m/%Y') AND DATE_FORMAT(STR_TO_DATE(data_final, '%Y-%m-%d'), '%d/%m/%Y')
try {
$stmt = $con->prepare($sql);
$stmt->execute($data_atual);
$comportamento = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $error) {
echo '<span class="box-error"><h5>Erro ao carregar comportamento_loja:' . '<span class="description-error">' .$error->getMessage(). '</span>' .'</span>';
}
$stmt = null;
You really have to use the
STR_TO_DATE
why don’t you send the date in formaty-m-d
straightforward?– adventistaam
actually not necessary, is that this was the option that worked when I did the query. thus: SELECT * FROM behavior_store Where enabled = 1 and '2018-07-05' BETWEEN (initialdata_) AND (finaldata_), in the right phpadmin but not in php, because I can’t quote the variable, where '2018-07-05' would be a date parameter today that will replace the ?
– Alexandre
and that $current date within $stmt is just like that?
– adventistaam
Post the whole function that makes the query
– adventistaam
This is the code: $data_current = date("d/m/Y"); $sql = "SELECT * FROM behavior_store Where enabled = 1 and ? BETWEEN DATE_FORMAT(STR_TO_DATE(data_inicial, '%Y-%m-%d'), '%d/%m/%Y') AND DATE_FORMAT(STR_TO_DATE(data_final, '%Y-%m-%d'), '%d/%m/%Y')

try { $stmt = $con->prepare($sql);
$stmt->execute($data_atual); $comportamento = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch(Pdoexception $error) { echo 'Error loading behavior_store:' . '' . $error->getMessage(). '' . '; } $stmt = null; . $data_current will replace the signal ?
– Alexandre
Try to convert the date to mysql format (Y-m-d) and take DATE_FORMAT
– adventistaam
How the dates are coming?
– adventistaam
Try closing the string of the $sql = "SELECT variable .... "
– adventistaam