简体   繁体   中英

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. I get the error message "mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource " . 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; rather, it should be 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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