使用变量时无法回显表值

时间:2015-03-07 23:38:32

标签: php mysql

我试图从我的桌子中称出2行来称为HK。这是我的代码

<?php require_once('Connections/hk.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$month_1 = "-1";
if (isset($_GET['Month'])) {
    $month_1 = $_GET['Month'];
}

$year_1 = "-1";
if (isset($_GET['year'])) {
    $year_1 = $_GET['year'];
}

mysql_select_db($database_hk, $hk);
$query_Recordset1 = "SELECT * FROM inventory WHERE 'Month' = '.$month_1' AND 'year' = $year_1";
$Recordset1 = mysql_query($query_Recordset1, $hk) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


$month_2 = "-1";
if (isset($_GET['Month2'])) {
    $month_2 = $_GET['Month2'];
}

$year_2 = "-1";
if (isset($_GET['year2'])) {
    $year_2 = $_GET['year2'];
}


mysql_select_db($database_hk, $hk);
$query_Recordset2 = "SELECT * FROM inventory WHERE 'Month' = Month AND year = $year_2";
$Recordset2 = mysql_query($query_Recordset2, $hk) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

mysql_select_db($database_hk, $hk);
$query_Recordset3 = "SELECT * FROM inventory_month";
$Recordset3 = mysql_query($query_Recordset3, $hk) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);
?>

当我尝试<?php echo $row_Recordset1['Month']; ?> <?php echo $row_Recordset2['year']; ?>时,我什么都没得到。我还用print_r($_GET);检查了我的var  我得到数组([Month1] =&gt; 12月[year1] =&gt; 2014 [Month2] =&gt; 1月[year2] =&gt; 2015 [提交] =&gt;查看)

因此,如果设置了我的var,为什么它不会回显月份和年份

1 个答案:

答案 0 :(得分:0)

更改

$query_Recordset1 = "SELECT * FROM inventory WHERE 'Month' = '.$month_1' AND 'year' = $year_1";

$query_Recordset1 = sprintf("SELECT * FROM inventory WHERE Month = %s AND year = $year_1", GetSQLValueString($month_1, "text"), GetSQLValueString($year_1, "text"));

$query_Recordset2 = "SELECT * FROM inventory WHERE 'Month' = Month AND year = $year_2";

$query_Recordset2 = sprintf("SELECT * FROM inventory WHERE Month = %s AND year = $year_2", GetSQLValueString($month_2, "text"), GetSQLValueString($year_2, "text"));