简体   繁体   English

如何使用 PHP 从 mysql 动态 select 表

[英]How to dynamically select a table from mysql using PHP

I created an html that has a formulary that let you choose between four different options:我创建了一个 html,它有一个让您在四个不同选项之间进行选择的公式:

<select name="especie">
  <option value = "1"> Hyundai </option>
  <option value = "2"> Renault </option>
  <option value = "3"> Ford </option>
  <option value = "4"> Fiat </option>
</select>

Each of these options correspond to one table in a mySQL database (let´s say it´s called companies), and have the same variables.这些选项中的每一个都对应于 mySQL 数据库(假设它称为公司)中的一个表,并且具有相同的变量。 The html then lets you perform the query, and I want it to display the information according to the table you have previously chosen. html 然后让您执行查询,我希望它根据您之前选择的表格显示信息。

$query = "SELECT * FROM $table WHERE brand LIKE ´%$brand%´;";

So what I want is to create a PHP that chooses dynamically the table ($table) depending on the chosen option by the user (in the first chuck of code).所以我想要的是创建一个 PHP 根据用户选择的选项(在第一个代码块中)动态选择表($table)。

Any help would be really appreciated it.任何帮助将不胜感激。

Without re designing your DB structure - which could be the best course of action, there are two easy ways to do this.在不重新设计数据库结构的情况下——这可能是最好的做法,有两种简单的方法可以做到这一点。 You could do it basically the way you said.你基本上可以按照你说的做。 If choosing this method you need to make sure that the table is being selected by a key from an array of tables so it is a safe input like so:如果选择此方法,您需要确保该表是由表数组中的一个键选择的,因此它是一个安全输入,如下所示:

$tables = ["table1" => "table1","table2" =>"table2"];
$query = "SELECT * FROM ".$tables[$table_key_from_input]." WHERE brand LIKE ´%$brand%´;";

But this is not ideal as, when tables are added removed all code like this needs to be updated.但这并不理想,因为当添加表时,所有类似这样的代码都需要更新。

Another way of getting the desired outcome would be to make a database view which is a union of all the tables (plus the table name in an extra column) then you can just select from the view every time using the table as a variable supplied by the user input.获得所需结果的另一种方法是创建一个数据库视图,它是所有表的联合(加上额外列中的表名),然后每次使用表作为由用户输入。

You can learn about views here: https://www.w3schools.com/sql/sql_view.asp您可以在此处了解视图: https://www.w3schools.com/sql/sql_view.asp

And see how to make a union here: https://www.w3schools.com/sql/sql_union.asp并在此处查看如何建立联合: https://www.w3schools.com/sql/sql_union.asp

The view code would look a little like this:视图代码看起来有点像这样:

CREATE VIEW view_name AS
SELECT *, 'table1' AS 'table' FROM table1
UNION
SELECT *, 'table2' AS 'table' FROM table2

Then your query would be like this:那么您的查询将是这样的:

$query = "SELECT * FROM view_name WHERE brand LIKE ´%$brand%´ AND table = $table;"; 

(But ideally using PDO!) (但理想情况下使用 PDO!)

Also if you do it this way if you ever re think your db structure and perhaps do what was suggested by El_Vanja in the comments you can, and by deleting the view and giving the table the same name the code wouldn't even have to change!此外,如果您这样做,如果您重新考虑您的数据库结构,并且可能执行 El_Vanja 在评论中建议的操作,并且通过删除视图并为表提供相同的名称,代码甚至不必更改!

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

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