【问题标题】:Can't echo tabel values when useing vararable使用变量时无法回显表值
【发布时间】:2015-03-09 10:20:40
【问题描述】:

我正在尝试从名为 HK 的表中比较 2 行。这是我的代码

<?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);
?>

当我尝试&lt;?php echo $row_Recordset1['Month']; ?&gt; &lt;?php echo $row_Recordset2['year']; ?&gt; 时,我什么也得不到。我还用 print_r($_GET); 检查了我的 var 我得到 Array ( [Month1] => December [year1] => 2014 [Month2] => January [year2] => 2015 [submit] => View )

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

【问题讨论】:

  • 你在哪里找到这个代码与测试if (PHP_VERSION &lt; 6) {
  • 您的 SQL 查询有误。你有"SELECT * FROM inventory WHERE 'Month' = '.$month_1' AND 'year' = $year_1",它应该是"SELECT * FROM inventory WHERE 'Month' = '$month_1' AND 'year' = $year_1"(注意$month_1前面的.
  • 您的第二个 SQL 查询也与 'Month' = Month 有错误,我认为您的意思是 'Month' = '$month_2'
  • 您的 GET 请求有“Month1”和“year1”但您的 PHP 正在寻找“Month”和“year”,这是第一个问题。我假设对于第二个查询,您按照我之前关于 'Month' = '$month_2' 的评论中的建议进行了更新。
  • @Turnerj 感谢您的所有帮助,而不仅仅是告诉我用谷歌搜索它。它已被修复。这是现在。 $query_Recordset1 = sprintf("SELECT * FROM inventory WHERE Month = %s AND year = $year_1", GetSQLValueString($month_1, "text"), GetSQLValueString($year_1, "text"));

标签: php mysql


【解决方案1】:

改变了

$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"));

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多