使用php

时间:2015-05-12 20:59:05

标签: php sql

好的所以我需要在数据库中搜索出生在两个日期之间的人的名字,网站要求用户输入这种格式的日期dd / mm / yyyy所以我需要操作才能使它工作在我的SQL搜索中。我做的是这个



$date_1 = $_GET["date_1"];
$date_2 = $_GET["date_2"];

function reorderDate($date) { //30/11/1970
  $firstbrk = strpos($date, '/');
  $day = substr($date, 0, $firstbrk);
  $month = substr($date, $firstbrk + 1, -5);
  $year = substr($date, strlen($date) - 4);
  $corrDate = $year.
  "-".$day.
  "-".$month;
  return $corrDate;
}

echo $Cdate_1 = reorderDate($date_1);
echo $Cdate_2 = reorderDate($date_2);

$tablename = 'Cyclist';
$sql = "SELECT name, dob FROM Cyclist WHERE dob > ' . $Cdate_1 . ' AND dob < ' . $Cdate_2 . ';";

$res = & $db - > query($sql);
if (PEAR::isError($res)) {
  die($res - > getMessage());
}
echo "<br>";
echo $findings_numb = $res - > numRows();
while ($row = $res - > fetchRow()) {
  echo $row[0].
  "\n";
}
&#13;
&#13;
&#13;

我没有在数据库中找到任何东西,但是如果我使用这一行进行搜索,我发现很多结果为什么?

&#13;
&#13;
$sql = "SELECT name, dob FROM Cyclist WHERE dob > '1970-30-11' AND dob < '1990-30-11';";
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

变化

$sql = "SELECT name, dob FROM Cyclist WHERE dob > ' . $Cdate_1 . ' AND dob < ' . $Cdate_2 . ';";

$sql = "SELECT name, dob FROM Cyclist WHERE dob > '" . $Cdate_1 . "' AND dob < '" . $Cdate_2 . "';";

答案 1 :(得分:0)

尝试这样的事情:

$sql = "SELECT name, dob FROM Cyclist WHERE dob > '$Cdate_1' AND dob < '$Cdate_2';";

当您使用双引号时,您不需要连接字符串。