从多个数据行PHP / MySQL中检索特定列值

时间:2014-01-08 01:59:02

标签: php mysql select limit between

我试图将一个查询放在一起,回显/打印表中的一个特定列值,但是在特定的时间范围内,但我没有太多运气。从下面的查询中,我试图在targets_set列下的targets_id = 1行中获取'267'来回显/打印。我收到错误消息“mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源”。如何让它回应?

<?php
$dealer = $_SESSION['sp_dealer_code'];
require_once ('/database.php');
$result = mysql_query("SELECT targets_set FROM targets WHERE targets_nmc='F80', sp_dealer_code=$dealer AND `targets_date`
BETWEEN '2014-01-01 00:00:00' AND '2014-01-31 23:59:59' LIMIT 1");
$row = mysql_fetch_assoc($result);
echo $row['targets_set'];
?>

数据库表'目标'和一些示例数据

targets_id  | sp_dealer_code | targets_nmc  | targets_set  | targets_actual | targets_date
1           | 1234           | F80          |  267         | 270            | 2014-01-01 01:00:00
2           | 1234           | F8R          |  350         | 300            | 2014-02-01 01:00:00
3           | 4567           | F80          |  210         | 200            | 2014-03-01 01:00:00
4           | 4567           | F8R          |  267         | 260            | 2014-01-01 01:00:00

1 个答案:

答案 0 :(得分:1)

您的查询已关闭。 WHERE A, B AND C无效MySQL;相反,它应该是WHERE A AND B AND C

SELECT targets_set
FROM targets
WHERE 
    targets_nmc='F80' 
    AND sp_dealer_code=$dealer 
    AND `targets_date` BETWEEN '2014-01-01 00:00:00' AND '2014-01-31 23:59:59'
LIMIT 1

执行语句时,不会返回任何结果集。这就是当您尝试将结果作为关联数组获取时收到该错误消息的原因。