我试图了解何时应该在 php/mysqli 中使用准备好的语句。每个 php/mysqli 查询是否应该使用准备好的语句,还是仅使用涉及用户输入的查询和实例……例如要求用户输入数据以在数据库中进行搜索的 html 表单?
我正在将旧的 php5/mysql 代码迁移到 php7/mysqli。我有很多查询 mysql 数据库的 php 文件。如果我需要对连接到 mysql 数据库的每个 php 文件使用准备好的语句,我想澄清一下...例如通过“php require”引用的 php 文件,并包含简单的 sql select 语句来渲染图像和链接到 html页?
<?php
//establish connection
$con = new mysqli('localhost','uid','pw','db');
//check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
//search variable that stores user input
$search = "%{$_POST['search']}%";
//prepare, bind and fetch
$stmt = $con->prepare("SELECT image, caption FROM `tblimages`
WHERE catid = 3 AND caption LIKE ? order by caption ASC");
$stmt->bind_param("s", $search);
$stmt->execute();
$stmt->bind_result($image,$caption); …Run Code Online (Sandbox Code Playgroud)