简体   繁体   English

SQL查询返回空结果

[英]SQL query returns empty result

I have this SQL query: 我有这个SQL查询:

SET @date_shift = DATE_SUB(NOW(), Interval 60 MINUTE);
SELECT hinfo.idx,
   hinfo.host_idx,
   hinfo.processor_load,
   hinfo.memory_total,
   hinfo.memory_free,
   hnames.idx,
   hnames.name,
   disks.hostinfo_idx,
   disks.id,
   disks.name,
   disks.size,
   disks.freespace,
   hinfo.probetime
FROM   systeminfo.hostinfo AS hinfo
   INNER JOIN systeminfo.hosts AS hnames
           ON hnames.idx = hinfo.host_idx
   INNER JOIN systeminfo.disksinfo AS disks
           ON disks.hostinfo_idx = hinfo.idx
WHERE   hinfo.probetime > @date_shift AND
      hinfo.probetime = (SELECT MAX(probetime) FROM systeminfo.hostinfo AS hi
                                    WHERE hi.probetime > @date_shift AND hi.host_idx = hinfo.host_idx)
GROUP  BY disks.id,
      hnames.name
ORDER  BY hnames.name,
      disks.id;

and i try to execute it in php code. 我试图在PHP代码中执行它。 I tried mysql, mysqli and pdo. 我尝试了mysql,mysqli和pdo。 Ma last code is following: 马最后的代码如下:

$db = new PDO("mysql:host=".$this->ip.";dbname=".$this->dbname.";charset=utf8", $this->username, $this->password);
$result = $db->query($query);
$fetch_data = $result->fetchAll(PDO::FETCH_ASSOC);

or 要么

mysql_connect($this->ip, $this->username, $this->password);
mysql_query('SET NAMES utf8;');
mysql_select_db($this->dbname);
$result = mysql_query($query);

PHP connects correcly to the database. PHP正确地连接到数据库。 Simple queries, like select * from tablename works. 简单的查询,例如从表名中选择*即可。 But this query returns NO data but error: 但是此查询不返回任何数据,但有错误:

1064 : You have an error in your SQL syntax; 1064:您的SQL语法错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT hinfo.idx, hinfo.host_idx, hinfo.processor_load, hinfo.memory_total, ' at line 2 在第2行的“ SELECT hinfo.idx,hinfo.host_idx,hinfo.processor_load,hinfo.memory_total,”附近检查与您的MySQL服务器版本相对应的手册以使用正确的语法

The same query, executed in the database client works perfectly (it's not a slow query, it takes less than 10 ms to perform it). 在数据库客户端中执行的同一查询可以完美地工作(这不是慢查询,执行此查询所需的时间少于10毫秒)。 Moreover, when i try to print the query directly from this function on a web page and paste it into mysql client - it works perfectly as well! 而且,当我尝试直接从网页上的此功能打印查询并将其粘贴到mysql客户端中时-它也​​很好地工作! I can't find any special character in hex viewer i tried to force utf-encoding, nothing. 我在尝试强制utf编码的十六进制查看器中找不到任何特殊字符,什么也没有。

Any ideas? 有任何想法吗? Test i could perform? 测试我可以执行? Thanks in advance! 提前致谢!

Try to execute this as two seperate statements. 尝试将其作为两个单独的语句执行。 Running queries from GUI tools or command line tools differ from embedding queries in code. 从GUI工具或命令行工具运行查询与将查询嵌入代码不同。 So it works in database tool, but not in php code. 因此,它可以在数据库工具中工作,但不能在php代码中工作。

The mysql adapter for PHP can only handle one query at a time. 用于PHP的mysql适配器一次只能处理一个查询。 You should indeed run the queries separate from each other. 您确实应该将查询彼此分开运行。

"@date_shift" will be available in the second query, assuming your connection to mysql does not get destroyed between the two queries. 假设您与mysql的连接在两个查询之间没有被破坏,则第二个查询中将使用“ @date_shift”。

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

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