简体   繁体   English

在涉及日期范围的MySQL / PHP查询方面需要帮助

[英]Need help with a MySQL/PHP query involving date ranges

Essentially I need to retrieve some account information from the dbase table where they have a client of the client that has been passed via GET, and the account placement date (dateplaced) is between the start and end dates passed from calendar fields again via GET. 从本质上讲,我需要从dbase表中检索一些客户信息,在该数据库中,他们具有已通过GET传递的客户的客户,并且帐户放置日期(已放置日期)介于再次通过GET从日历字段传递的开始和结束日期之间。

The query below returns no rows. 下面的查询不返回任何行。 I have verified that the SELECT and FROM portions of the query work as intended, and the client select from GET works fine, so that leaves the date issues. 我已经验证查询的SELECT和FROM部分可以按预期工作,并且客户端从GET中进行选择可以正常工作,因此不会出现日期问题。 "dateplaced" is stored in the database as a varchar in "dd/mm/yyyy" format. “ dateplaced”作为varchar以“ dd / mm / yyyy”格式存储在数据库中。 Upon researching I've found that mysql wants dates in "yyyy-mm-dd" format. 经过研究,我发现mysql希望使用“ yyyy-mm-dd”格式的日期。 I have pieced together the two parts of the between comparison from GET data, and am fairly certain they are formatted correctly. 我已经将GET数据之间的比较的两个部分拼凑在一起,并且可以肯定它们的格式正确。 I tried just plainly using "dateplaced" instead of str_to_date() and it didn't work, so I tried str_to_date() as shown below, but it still doesn't work. 我只是尝试使用“ dateplaced”代替了str_to_date(),但它没有用,所以我尝试了str_to_date(),如下所示,但仍然不起作用。

Can anyone spot my problem here? 有人可以在这里发现我的问题吗? This is driving me nuts. 这让我发疯。

$query = mysql_query("SELECT accountnumber, firstname, middlename, lastname, currentbalance FROM dbase WHERE clientname = '" . $_GET['client'] . "' AND str_to_date(dateplaced, '%Y-%m-%d') BETWEEN '" . $_GET['calendar_start_year'] . "-" . $_GET['calendar_start_month'] . "-" . $_GET['calendar_start_day'] . "' AND '" . $_GET['calendar_end_year'] . "-" . $_GET['calendar_end_month'] . "-" . $_GET['calendar_end_day'] . "' ORDER BY lastname") or die(mysql_error());

str_to_date() takes the format of the existing string, not the desired string. str_to_date()采用现有字符串的格式,而不是所需的字符串。 So you need str_to_date(dateplaced, '%d/%m/%Y') to get Ymd. 因此,您需要str_to_date(dateplaced, '%d/%m/%Y')来获取Ymd。

Do your date creation outside the query. 在查询之外创建日期。 It's easier to debug. 调试起来更容易。

$clean_name=mysqli_real_escape_string($_GET['client']);
$start_date=date_create("{$_GET['calendar_start_year']}-{$_GET['calendar_start_month']}-{$_GET['calendar_start_day']}")->format('Y-m-d');
$end_date=date_create("{$_GET['calendar_end_year']}-{$_GET['calendar_end_month']}-{$_GET['calendar_end_day']}")->format('Y-m-d');

$q="SELECT accountnumber, firstname, middlename, lastname, currentbalance 
FROM dbase 
WHERE clientname = '{$clean_name}' 
  AND str_to_date(dateplaced, '%d/%m/%Y') BETWEEN '$start_date' AND '$end_date' 
ORDER BY lastname;

PS, clean all your variables. PS,清理所有变量。 There may be quote marks or other characters that need to be escaped (or evil hacker attempts). 可能有引号或其他字符需要转义(或邪恶的黑客尝试)。 mysqli_real_escape_string() is what you should use. 应该使用mysqli_real_escape_string()

First of all, you have left yourself wide-open to SQL injection by not escaping your user variables before inserting them into your query (see mysql_real_escape_string ). 首先,通过在将用户变量插入查询中之前不转义用户变量,您可以自由地进行SQL注入(请参阅mysql_real_escape_string )。

The reason your query isn't working is because STR_TO_DATE 's second parameter needs the format of your source string, not the format you are trying to convert to. 您的查询不起作用的原因是因为STR_TO_DATE的第二个参数需要源字符串的格式,而不是您尝试转换为的格式。

// extract variables
$client = $_GET['client'];

$startYear = $_GET['calendar_start_year'];
$startMonth = $_GET['calendar_start_month'];
$startDay = $_GET['calendar_start_day'];

$endYear = $_GET['calendar_end_year'];
$endMonth = $_GET['calendar_end_month'];
$endDay = $_GET['calendar_end_day'] ;

// parse variables
$startDate = mktime(0, 0, 0, $startMonth, $startDay, $startYear);
$endDate = mktime(0, 0, 0, $endMonth, $endDay, $endYear);

// query database
$sql = sprintf(
   "SELECT accountnumber,
           firstname,
           middlename,
           lastname,
           currentbalance
    FROM dbase
    WHERE clientname = '%s'
    AND STR_TO_DATE(dateplaced, '%d/%m/%Y') BETWEEN '%s' AND '%s'
    ORDER BY lastname",
    mysql_real_escape_string($client),
    date('Y-m-d', $startDate),
    date('Y-m-d', $endDate));

$result = mysql_query($sql) or die(mysql_error());

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

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