【问题标题】:SQL returns result with 0 rowsSQL 返回 0 行的结果
【发布时间】:2013-12-07 15:13:36
【问题描述】:
    function getData()
    {
        $select_query = "SELECT Value FROM Allies WHERE 'Key'='14638'";

        mysqli_real_escape_string($this -> dataConnection, $this -> cacheKey);
        mysqli_real_escape_string($this -> dataConnection, $this -> id);

        if ($result = $this -> dataConnection ->query($select_query)) {
            printf("Select returned %d rows.\n", $result->num_rows);

            /* free result set */
            $result->close();
        }

    }

由于某种原因,此代码在数据存在时打印“选择返回 0 行”,而在数据不存在时不打印任何内容。

这是数据的截图,如果有帮助的话:

如何修复它并访问数据?

【问题讨论】:

  • 尝试删除查询 SELECT 中 'Key' 周围的单引号。
  • 你没有捕捉到那些mysqli_real_escape_strings 的结果。而且最好使用prepared statements

标签: php sql mysqli


【解决方案1】:

Key 是保留字。所以用反引号代替引号:

改变这个:

 $select_query = "SELECT Value FROM Allies WHERE 'Key'='14638'";

收件人:

 $select_query = "SELECT Value FROM Allies WHERE `Key`='14638'";

【讨论】:

  • 查询停止返回任何内容。
  • 键为保留字,请使用反引号。
猜你喜欢
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 2017-02-22
  • 2013-04-27
相关资源
最近更新 更多