Php MySQLi中的参数

Php MySQLi中的参数,php,parameters,mysqli,Php,Parameters,Mysqli,我在MySQLi中使用PHP,我遇到了如下查询 SELECT $fields FROM $table WHERE $this=$that AND $this2=$that2 到目前为止,我已经编写了一些代码来拼接我提供的数组,例如: $search = array(name=michael, age=20) //turns into SELECT $fields FROM $table WHERE name=michael AND age=20 有没有更有效的方法 我相当担心MySQL注入—

我在MySQLi中使用PHP,我遇到了如下查询

SELECT $fields FROM $table WHERE $this=$that AND $this2=$that2
到目前为止,我已经编写了一些代码来拼接我提供的数组,例如:

$search = array(name=michael, age=20) //turns into
SELECT $fields FROM $table WHERE name=michael AND age=20
有没有更有效的方法

我相当担心MySQL注入——这似乎非常脆弱。
谢谢

奇怪的是,你问题的标题基本上就是它的答案。您希望使用mysqli参数化查询执行以下操作:

$db = new mysqli(<database connection info here>);
$name = "michael";
$age = 20;

$stmt = $db->prepare("SELECT $fields FROm $table WHERE name = ? AND age = ?");
$stmt->bind_param("si", $name, $age);
$stmt->execute();
$stmt->close();
$db=newmysqli();
$name=“迈克尔”;
$age=20;
$stmt=$db->prepare(“从$table中选择$fields,其中name=?和age=?”;
$stmt->bind_参数(“si”、$name、$age);
$stmt->execute();
$stmt->close();
中的更多信息,特别是与相关的功能

请注意,我个人更喜欢使用而不是mysqli,我不喜欢mysqli做的所有
bind_参数
/
bind_结果
东西。如果我必须使用它,我会在它周围写一个包装器,使它更像PDO