简体   繁体   English

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

[英]Retrieving a particular column value from multiple data rows PHP/MySQL

I trying to put a query together which echo/prints one particular column value from within the table, but within a particular time frame but I'm not having much luck. 我试图将一个查询放在一起,该查询从表中但在特定时间范围内回显/打印一个特定的列值,但运气不佳。 From the Query below, Im trying to get the '267' in the targets_id=1 row under the targets_set column to echo/print. 从下面的查询中,我试图在targets_set列下的targets_id = 1行中获取“ 267”以进行回显/打印。 I get the error message "mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource " . 我收到错误消息“ mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源” How do I get it to echo? 我如何使其回声?

<?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'];
?>

The Database table 'targets' and some sample data 数据库表“目标”和一些示例数据

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

Your query is off. 您的查询已关闭。 WHERE A, B AND C is not valid MySQL; WHERE A, B AND C在无效的MySQL中; rather, it should be WHERE A AND B AND C : 而是应该在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

When the statement is executed, no result set is given back. 执行该语句时,不会返回任何结果集。 That's why you get that error message when you try to fetch the result as an associative array. 这就是为什么当您尝试以关联数组的形式获取结果时收到错误消息的原因。

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

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