0

我有在 mysql 中运行良好的 sql 语句,我正在使用 datediff。当我尝试在 php 中使用它时,我得到一个“mysql_fetch_arrary() 期望参数 1 是一个资源,boolean given in”

这个说法是...

$result = mysql_query("select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) from host where stype='physical';",$db);

我知道该语句在 mysql 中有效

mysql> select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) from servers.host where stype='physical';
+--------------+---------+--------+-----------+-------+------------+-------------------------+
| hname        | hsn     | hmodel | hmake     | htype | hwar       | datediff(`hwar`, now()) |
+--------------+---------+--------+-----------+-------+------------+-------------------------+
| moscow       | XXXXXXX | Dell   | PowerEdge | R710  | 2013-09-13 |                     225 |
| sydney       | XXXXXXX | Dell   | PowerEdge | R710  | 2013-09-15 |                     227 |

当我删除datediff(hwar, now())时,我的页面可以工作。我想用它作为字段

$datediff=$row['datediff'];

关于为什么它不起作用的任何线索?

4

3 回答 3

0

尝试限制您的查询

   $result = mysql_query("select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) as dif from host where stype='physical' LIMIT 0,10 ;",$db);

这个帖子

于 2013-01-31T18:52:01.017 回答
0

试试这个:

替换这条sql语句:datediff( hwar, now())

用:datediff( hwar, now()) 作为 Diffdate

并以这种方式恢复它: $datediff=$row['Diffdate'];

再见XD

于 2013-01-31T18:53:58.610 回答
0

你应该给选择器一个别名:

select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) AS diff from host where stype='physical';

然后您可以访问名为“diff”的列

于 2013-01-31T18:54:11.633 回答