使SQL识别来自PHP / html选择选项的变量
I think the answer to this may be simple, but being new to SQL I am still growing. Here's my dilemma. I have a php array of options with 10 values. When any one option is selected it is passed into a variable named "spots". I have 10 SQL SELECT statements that pull 1 of 10 different tables. The issue is that I do not know exactly what to do in order to get the SQL to recognize which value was selected and based on which was selected show that specific table data. (This would be easy if I were able to use the JavaScript Switch statement, but I do not know an equivalent for that)
EXAMPLE:
PHP
$spots = ["Report1","Report2","Report3","Report4","Report5","Report6","Report7","Report8","Report9","Report10"];
SQL
SELECT *, FROM Report5
ORDER BY TW ASC;
Now how do I get SQL to loop through an array to find a match, then depending on that match select from a list of commands (for example like a JavaScript switch statement)?
我认为这个问题的答案可能很简单,但对于SQL新手我还在增长。 这是我的困境。 我有一个包含10个值的php数组选项。 当选择任何一个选项时,它将被传递到名为“斑点”的变量中。 我有10个SQL SELECT语句,可以拉出10个不同表中的1个。 问题是我不知道该做什么,以便让SQL识别选择了哪个值,并根据选择哪个值显示特定的表数据。 (如果我能够使用JavaScript Switch语句,这将很容易,但我不知道它的等价物) p>
示例: p>
PHP p>
$ spots = [“Report1”,“Report2”,“Report3”,“Report4”,“Report5”,“Report6”,“Report7”,“Report8” ,“Report9”,“Report10”];
code> pre>
SQL p>
SELECT *,FROM Report5
ORDER BY TW ASC;
code> pre>
现在我如何让SQL循环遍历数组以找到匹配项,然后根据匹配项从命令列表中选择(例如 像JavaScript switch语句)? p>
div>
Use variable substitution:
foreach ($spots as $spot) {
$sql = "SELECT * FROM $spot ORDER BY TW ASC";
// perform the SQL query using $sql, do what you want with the results
}
Make sure you've validated that the values in $spots
are valid if they're coming from the user. Otherwise you'll be subjecting your code to SQL injection.