简体   繁体   中英

Comparing dates mysql/php doesn't work

I have a problem. I have a SQL sentence and it works on phpMyAdmin, but on php it gives a null value.

SQL sentence with correct values:

SELECT * FROM `CON_DECKEL` WHERE fecha < (SELECT STR_TO_DATE('2017-03-15T15:53','%Y-%m-%dT%H:%i')) LIMIT 3;

PHP code with null values:

$sql = "SELECT * FROM `CON_DECKEL` WHERE fecha < (SELECT STR_TO_DATE('2017-03-15T15:53','%Y-%m-%dT%H:%i')) LIMIT 3;";

echo htmlspecialchars($sql);

if ($resultado = $conn->query(htmlspecialchars($sql))) {
    $query_success = true;
    $valores = array();
    var_dump($valores);
    while ($row = $resultado->fetch_object())
    {
        array_push($valores, array($row->canal1, $row->canal2, $row->canal3, $row->canal4, $row->fecha));
    }
    $resultado->close();
}
var_dump($valores);
$conn->close();
} else {
$msg = "Error de conexión: " . $conn->connect_error;
}

I get the SQL sentence with "echo htmlspecialchars($sql);", I paste this sentence on PHPMyAdmin and it works perfectly.

Can anyone help me? I don't know why it doesn't work on PHP and I don't know how to fix it.

Thanks! :)

Seeing as you figured out what the issue is yourself, I'm just adding an answer to clarify a few things.

It is perfectly fine to use htmlspecialchars() , however only on user submitted values.

The reason why it failed is you were using it on the full query, translating < to &lt; , something SQL doesn't understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM