简体   繁体   English

在PHP中生成动态的MYSQLi准备语句

[英]Generate a dynamic MYSQLi prepared statement in PHP

I have an app that displays a list of files from a database table. 我有一个显示数据库表中文件列表的应用程序。 I would like the user to be able to create a group by choosing options that will filter the list of files and only show the ones that match. 我希望用户能够通过选择将过滤文件列表并仅显示匹配文件的选项来创建组。 The group options are added to another table for later use. 组选项将添加到另一个表中以供以后使用。

Here are the options: name year type room day time 以下是选项: name year type room day time

All of them apart from name are optional and can be left blank by the user. name外,它们都是可选的,用户可以将其留空。 Some of the options can contain multiple values seperated by a , . 某些选项可以包含多个以,分隔的值。

Here is my code to add this information: 这是我的代码以添加此信息:

if ($addGrp = $m->prepare("SELECT * FROM pro_files WHERE userid=?, name=? ...")) {
    $addGrp->bind_param('is ...',$userid, $name ...;
}

For reference, to select these dymanic columns I'll be using: 作为参考,选择以下动态列:

WHERE room in (g23,f43,g43) AND type in ('pptx,ppt,ai,pdf')

and the variables used to bind them are comma separated values as such (generated based on what the user chooses, the variable won't exist if the user chose nothing): 并且用于绑定它们的变量是这样的逗号分隔值(根据用户选择的内容生成,如果用户未选择,则该变量将不存在):

$room = 'g23,f43,g43';
$type = 'pptx,ppt,ai,pdf';

Using the old MYSQL method I could have just created a query string no problem. 使用旧的MYSQL方法,我本来可以创建查询字符串没问题。 How do I create a dynamic query when I also have the bind_param line to deal with? 当我还需要处理bind_param行时,如何创建动态查询?

For demonstration I've added ... where I need to dynamically generate the query. 为了演示起见,我添加了... ,在这里我需要动态生成查询。

I can't use PDO due because the rest of the app is currently using MYSQLi, the job would be too great. 由于该应用程序的其余部分当前正在使用MYSQLi,因此我无法使用PDO,因此这项工作将非常艰巨。

Alternatively, is it possible to use a wildcard in an IN clause? 另外,是否可以在IN子句中使用通配符? I could just make '' = '*' within PHP in that case. 在这种情况下,我可以在PHP中使用'' = '*' However room in ('*') doesn't work. 但是room in ('*')中的空格不起作用。

Well, with vanilla mysqli prepared statements it would be quite a hard challenge. 好吧,使用香草mysqli准备的语句将是一个相当艰巨的挑战。
But with some DBAL or ORM it could be simplified. 但是使用某些DBAL或ORM可以简化它。 Say, by using a DBAL I wrote , it could be done in a quite neat way: 说,通过使用我编写DBAL ,可以用一种非常简洁的方式来完成它:

First define your variables based on user input. 首先根据用户输入定义变量。

$name = 'Bill';
$year = NULL;
$room = ['g23','f43','g43'];
$type = NULL;
$day  = 4;
$time = NULL;

Then write a query where every variable is used twice 然后编写一个查询,其中每个变量使用两次

$sql = "SELECT * pro_files WHERE name=?s 
(AND ?s is NULL OR year = ?s)
(AND ?a is NULL OR type = ?a)
(AND ?a is NULL OR room = ?a)
(AND ?s is NULL OR day  = ?s)
(AND ?s is NULL OR time = ?s)";

And finally run the query: 最后运行查询:

$data = $db->getAll($sql,$name,$year,$year,$room,$room,$type,$type,$day,$day,$time,$time);

It will return the search result based on non-null values provided. 它将基于提供的非空值返回搜索结果。

I only have to mention that prepared statements are only emulated here, but it is as safe, as native ones. 我只需要提一下,仅在此处模拟了准备好的语句,但它与本地语句一样安全。

By the way, I would like to see an approach from other DBAL or ORM. 顺便说一下,我想看看其他DBAL或ORM的方法。 To recommend one is one thing and to show a working example is another. 推荐一个是一回事,展示一个可行的例子是另一回事。

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

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