【问题标题】:mysql query based on php variable基于php变量的mysql查询
【发布时间】:2017-10-26 08:50:52
【问题描述】:

这是我的代码

  $query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger,jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where education.Abschluss in ('Hauptschulabschluss') and recipients.Bundesland = '.$_REQUEST['land'].'";
$result=mysqli_query($db, $query) or die('Error querying database.');

$q = "select ((education.weiblich+education.maennlich)/inhab.Einwohner) as 'niedriger Bildungsstand',Jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr WHERE education.Abschluss in ('Ohne Haupschulabschluss','Hauptschulabschluss') and recipients.Bundesland = '.$_REQUEST['land'].'";
$r=mysqli_query($db, $q) or die('Error querying database.');

$_REQUEST['land']。我正在尝试获取下拉菜单的选定值。可变土地正在工作,我可以毫无问题地回应它。没有$_REQUEST['land'],查询也可以正常工作。

但现在我收到 500 错误。你知道我在这里做错了吗?

【问题讨论】:

    标签: javascript php mysql


    【解决方案1】:

    你应该转义你的变量以用双引号连接(当你用双引号开始你的字符串时),改变这个:

    $q = "... and recipients.Bundesland = '.$_REQUEST['land'].'";
    

    到这里:

    $q = "... and recipients.Bundesland = '".$_REQUEST['land']."'";
    

    第一个查询也是如此。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      我认为问题可能出在你的查询行上,你用双引号打开,然后用单引号结束

       $query = "select.... '.$_REQUEST['land'].'";
      

      尝试简单地使用这个:

       $query = "select.... ".$_REQUEST['land'];
      

      【讨论】:

        【解决方案3】:

        试试看:

        $query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger,jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where education.Abschluss in ('Hauptschulabschluss') and recipients.Bundesland = '".$_REQUEST['land']."'";
        $result=mysqli_query($db, $query) or die('Error querying database.');
        
        $q = "select ((education.weiblich+education.maennlich)/inhab.Einwohner) as 'niedriger Bildungsstand',Jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr WHERE education.Abschluss in ('Ohne Haupschulabschluss','Hauptschulabschluss') and recipients.Bundesland = '".$_REQUEST['land']."'";
        $r=mysqli_query($db, $q) or die('Error querying database.');
        

        我认为它有效。 :) ,我只是在查询中替换 ' 为 "

        【讨论】:

          【解决方案4】:

          连接问题,总是喜欢使用花括号来避免这些问题:

          Curly Brackets {} 花括号用于标记类、函数(OOP 术语中的方法)、循环和控制结构体。

          它们也可以在字符串中用于将变量与周围的文本分开。

          1。 $verb = '添加'; 2. echo "这个动词的现在时是 $verb"; 认为您想显示动词的过去时而不重新定义它(只需添加“ed”)。

          1。 echo "这个动词的过去式是 $verbed"; 如果您尝试上述方式,那么 PHP 将搜索变量 $verbed 并抛出错误(因为它没有定义)。要将动词与后缀“ed”分开,可以使用大括号,如下所示。

          1。 echo "这个动词的过去式是 {$verb}ed"; 如果 $verb 是一个数组,它的一个元素可以像下面这样使用。

          1。 echo "这个动词的过去式是 {$verb['past_tense']}"; 如果 $verb 是一个对象并且有一个名为 getPastTense() 的方法返回动词的过去时,它可以像下面这样使用。

          1。 echo "这个动词的过去式是 {$verb->getPastTense()}";

           $query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger,jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where education.Abschluss in ('Hauptschulabschluss') and recipients.Bundesland = '{$_REQUEST['land']}'";
          $result=mysqli_query($db, $query) or die('Error querying database.');
          
          $q = "select ((education.weiblich+education.maennlich)/inhab.Einwohner) as 'niedriger Bildungsstand',Jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr WHERE education.Abschluss in ('Ohne Haupschulabschluss','Hauptschulabschluss') and recipients.Bundesland = '{$_REQUEST['land']}'";
          $r=mysqli_query($db, $q) or die('Error querying database.');
          

          Curly Braces Notation in PHP

          【讨论】:

            【解决方案5】:

            您可以将字符串双引号并在双引号内使用 {$var}。使其更具可读性。

            $query = "... and recipients.Bundesland = '{$_REQUEST['land']}'";

            此外,我建议您尝试 PDO 或 NotORM。至少通过一些对其进行清理的东西来拉动您的 GET/POST/REQUEST。

            【讨论】:

              猜你喜欢
              • 2014-11-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-10-16
              相关资源
              最近更新 更多