简体   繁体   English

PHP-mysql 查询中的多个变量

[英]PHP- Multiple variables in mysql query

I want to get the value from the Epic[2] column.我想从 Epic[2] 列中获取值。

The mysql table looks like that: mysql 表如下所示:

ID  |  Epic[0] |  Epic[1] |  Epic[2] |  Epic[3]
-----------------------------------------------
440     xy        xy         xy           xy

I have three variables where the informations are stored.我有三个存储信息的变量。

$qualitybudget = "Epic"
$budgetType = "2"
$itemlevel = "440"

I tried the following:我尝试了以下方法:

$sql = "SELECT '$qualitybudget'['$budgetType'] FROM `randproppoints` WHERE id = '$itemlevel'";

Output looks like that:输出看起来像这样:

SELECT 'Epic'['2'] FROM `randproppoints` WHERE id = '440'

As you see 'Epic'['2'] has the wrong syntaxing.如您所见, 'Epic'['2']的语法错误。 The output for the working query should be like: Epic[2]工作查询的输出应类似于: Epic[2]

What is the right syntax handling in a mysql query like that?像这样的 mysql 查询中正确的语法处理是什么?

Use

$sql = "SELECT `${qualitybudget}[$budgetType]` FROM `randproppoints` WHERE id = '$itemlevel'";

instead.反而。 This way, the column name is exactly formatted as you wish and encapsulated.通过这种方式,列名完全按照您的意愿格式化并封装。

Note that the syntax ${xyz} has to be used to make it clear that only xyz is the name of the variable and that this is all to be printed here.请注意,必须使用语法${xyz}来明确只有xyz是变量的名称,并且所有这些都将在此处打印。 Otherwise, PHP assumes it is an array and evaluates the following squared brackets for the same string insertion.否则,PHP 假定它是一个数组,并为相同的字符串插入评估以下方括号。 Another example where this syntax has to be used is if for instance you had a variable $a = "thing";另一个必须使用此语法的示例是,例如您有一个变量$a = "thing"; and wanted并且想要

$b = "there are three $as";

to get "there are three things" .得到"there are three things" It would not work as PHP would assume you address a variable $as , but the fix would seem like this:它不会工作,因为 PHP 会假设您寻址变量$as ,但修复程序看起来像这样:

$b = "there are three ${a}s";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM