Query that works in SQL and not in PHP

0

I have a code like this:

$consulta= "select campo1, campo2, ..
 from tabla 
where fecha >'2017-10-12 9:42' 
order by fecha asc";
echo $consulta; /*lo tengo para hacer pruebas */
$filas = $db->query($consulta);
if (!$filas) {
        $error=$db->errorInfo();
        return "Error en la consulta. Error ". $error[2];
    } else {
        if ($filas->rowcount()==0) echo "<span>No hay resutados</span>";
        else
        foreach($filas as $fila){
                   .................
        }

Date is of type datetime .

The issue is that when I run the code on the server it gives me a result (no row) and yet if I copy what is printed in the echo (the query) and execute it directly using the editor in phpMyAdmin I gives the result it should (at this moment 2 rows come out).

When I change the symbol of > by < (to see the previous ones to the date) executing the php it removes all the rows to me. If I paste the query, it does it correctly and only removes the events prior to the date.

This same code has been tested with the function now() of SQL instead of the written date (my idea is to extract it with javascript of the equipment that uses the application) and it works correctly. My problem is that the server time is different and therefore takes out future events hours after they have already passed.

The question is if something similar has happened to someone (a query that works in SQL and not through the query() method and how it has been solved.

    
asked by user62709 12.10.2017 в 10:10
source

2 answers

0

I have continued doing tests and I have seen that the error is not in the code that I have put. I was deceived by the fact that the query is printed .. The error is in the creation of that date since I do it in javascript and I am realizing that it was not created when the query is executed (apparently it is comparing with a empty string), is a problem of times. I'll keep trying out there ..

    
answered by 12.10.2017 в 17:00
0

If the SQL query returns results in phpMyAdmin, almost certainly something is wrong in the logic of your program. Doing a basic debug would help you to easily find the error, or at least to discard ... For example, your code could be replaced by something like:

$consulta= "select campo1, campo2, ..
 from tabla 
where fecha >'2017-10-12 9:42' 
order by fecha asc";

echo $consulta; /*lo tengo para hacer pruebas */
$filas = $db->query( $consulta );
var_dump( $filas );
if ( ! $filas) {
    die( "No hay resultados" );

    $error=$db->errorInfo();
    return "Error en la consulta. Error ". $error[2];

} else {
    die( "Hay resultados" );

    if ($filas->rowcount()==0) 
        echo "<span>No hay resutados</span>";
    else
        foreach($filas as $fila){
            ...
        }

PS: sorry, it's too late to answer. My laptop battery died while I was answering a few hours ago ...

    
answered by 12.10.2017 в 20:02