简体   繁体   中英

Why this query is always working in phpmyadmin but on my php pdo code it works sometimes?

I'm trying to get the rows that was added in the last 35 minutes. The field agendadoEm is of type datetime(YYYY-MM-DD HH:ii:ss). When I copy the exactly same query and paste to phpmyadmin it works properly but in my php code using pdo it works according to its own desire(lol). Some times it returns properly and in few seconds it return no results anymore, then works again. No errors are returned, also var_dump($_SESSION['userid']) is returning the expected value.

My php code:

function db_exec($conn,$sql,$values=null){
    $sth = $conn->prepare($sql);
    if($values!=null)
        foreach($values as $key => $value){
            $index=++$key;
            $sth->bindValue($index,$value);
    }
    $sth->execute();
    return $sth;
}

$agendamentos=db_exec($dbread,"SELECT agendamentos.id as id, agendamentos.userid as userid, 
agendamentos.paciente as paciente,agendamentos.agendadoPara as agendadoPara,usuarios.nome as nome,
usuarios.contaTipo as contaTipo, profissionais.sexo as sexo, profissionais.profissao as profissaoId,
tipoProfissionalPF.tipo as profissao,tipoProfissionalPF.urlprefix as urlprefix, tipoProfissionalPJ.tipo as estabelecimento,
tipoProfissionalPJ.urlprefix as clinprefix,empresaDados.nomeFantasia as nomeFantasia
FROM agendamentos 
LEFT JOIN usuarios ON usuarios.id=agendamentos.userid 
LEFT JOIN profissionais ON profissionais.userid=agendamentos.userid 
LEFT JOIN empresaDados ON empresaDados.userid=agendamentos.userid 
LEFT JOIN tipoProfissionalPF ON tipoProfissionalPF.id=profissionais.profissao 
LEFT JOIN tipoProfissionalPJ ON tipoProfissionalPJ.id=empresaDados.tipoProfissionalPJ 
WHERE agendamentos.paciente=? AND agendamentos.confirmado=0 AND TIMESTAMPDIFF(MINUTE,agendamentos.agendadoEm,NOW())<35",array($_SESSION['userid']));

if($agendamentos->rowCount()>0) 
  echo 'ok';
else 
  echo 'none';

As I said before, sometimes it works and in minutes it doens't work any more. By running this query on phpmyadmin always works, even when it's not working on my php page. Does anyone have an idea of what I'm doing wrong? For me is a mistery, I'm not identifying the problem.

The problem was the timezone of my rds instances. The master rds was setted to a different timezone of the slave, so the query could have two diferent results for NOW(), depeding on which rds was being reached.

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