基于php变量的mysql查询

时间:2017-05-25 16:05:05

标签: javascript php mysql

这是我的代码

  $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错误。你知道我在这里做错了什么吗?

5 个答案:

答案 0 :(得分:1)

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

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

对此:

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

第一个查询相同。希望它有所帮助。

答案 1 :(得分:0)

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

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

尝试使用这个:

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

答案 2 :(得分:0)

试一试:

$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.');

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

答案 3 :(得分:0)

连接问题,总是更喜欢使用Curly Brackets来避免这些问题:

  

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

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

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

1。 回声"这个动词的过去时是$ verbed&#34 ;; 如果您尝试上述方法,那么PHP将搜索变量$ verbed并抛出错误(因为它未定义)。要将动词与后缀“ed”分开,可以使用如下的花括号。

1。 echo"这个动词的过去时是{$ verb} ed&#34 ;; 如果$ verb是一个数组,则可以使用它的一个元素,如下所示。

1。 回声"这个动词的过去时是{$ verb [' past_tense']}&#34 ;; 如果$ 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

答案 4 :(得分:0)

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

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

我还建议您尝试使用PDO或NotORM。至少通过消毒它来获取你的GET / POST / REQUEST。