简体   繁体   English

为什么在php中没有返回结果,但是在mysql中有?

[英]Why is there no results returned in php, but there are in mysql?

Query: 查询:

SELECT * FROM ac_journal WHERE datetime < (SELECT datetime FROM ac_journal WHERE jid = 1089 AND rid IN ('2','3','4')) ORDER BY datetime DESC LIMIT 1

When I execute this query in phpmyadmin, I get a row 当我在phpmyadmin中执行此查询时,我得到一行 在此输入图像描述

In my code, it is not returning anything. 在我的代码中,它没有返回任何东西。

function ac_journal_get_previous_entry($jid)
{
  global $user; //1
  echo $jid; //1089
  echo "<br />'" . implode("','", array_keys($user->roles)) . "'"; //'2','3','4'

  if ($jid > 0)
  {
    $result = db_query("SELECT * FROM {ac_journal} WHERE datetime < (SELECT datetime FROM {ac_journal} WHERE jid = ? AND rid IN (?)) ORDER BY datetime DESC LIMIT 1", array($jid, "'" . implode("','", array_keys($user->roles)) . "'"));
    $obj = $result->fetchAll();
    echo $result->rowCount(); //0

    if (is_array($obj) && count($obj) > 0)
    { 
      echo "HERE"; //never gets here
      return "<a href='https://example.com/journal/" . $obj[0]->jid . "'>Previous</a>";
    }
  }
}

我不得不改变它:

$result = db_query('SELECT * FROM {ac_journal} WHERE datetime < (SELECT datetime FROM {ac_journal} WHERE jid = :jid) AND rid IN (:rid) ORDER BY datetime DESC LIMIT 1', array(':jid' => $jid, ':rid' => array_keys($user->roles)));

this line makes no result in echo: fetchAll(); 这行不会导致echo: fetchAll(); as in; 如;

$obj = $result->fetchAll();

try to change it into: 尝试将其更改为:

fetch(PDO::FETCH_ASSOC);

But, Execute first the array before fetching the object, as in: 但是,在获取对象之前首先执行数组,如:

$obj->execute(array(':jid' => $jid ));
$result = $obj->fetch(PDO::FETCH_ASSOC);

if (is_array($result) && count($result) > 0){ 
     echo "HERE"
.................

Source: http://php.net/manual/en/pdostatement.fetch.php 资料来源: http//php.net/manual/en/pdostatement.fetch.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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