php mysqli准备语句
嘿,我有一个快速的。有没有办法将变量包含到准备好的查询中?例如:php mysqli准备语句,php,mysqli,prepared-statement,Php,Mysqli,Prepared Statement,嘿,我有一个快速的。有没有办法将变量包含到准备好的查询中?例如: $sql = "SELECT id, title, author, LEFT(description, 40) AS excerpt, image_small, image_med, date FROM posts ORDER BY id DESC LIMIT $start, $postsPerPage"; $result = $connect
$sql = "SELECT id, title, author, LEFT(description, 40) AS excerpt,
image_small, image_med, date
FROM posts
ORDER BY id DESC
LIMIT $start, $postsPerPage";
$result = $connect->prepare($sql) or die ('error');
$result->execute();
$result->bind_result($id, $title, $author, $excerpt, $image_small, $image_med, $date);
谢谢 如果我没有弄错的话,您必须使用bindParam并用问号替换查询中的变量
$sql = "SELECT id, title, author, LEFT(description, 40) AS excerpt,
image_small, image_med, date
FROM posts
ORDER BY id DESC
LIMIT ?, ?";
$result = $connect->prepare($sql) or die ('error');
$result->bindParam(1, $start);
$result->bindParam(2, $postsPerPage);
您可以在找到更多示例,您需要以下内容:
$start = 1; $postsPerPage = 1;
$sql = "SELECT id, title, author, LEFT(description, 40) AS excerpt,
image_small, image_med, date
FROM posts
ORDER BY id DESC
LIMIT ?, ?";
$stmt = $connect->prepare($sql) or die ('error');
$stmt->bind_param('ii', $start, $postsPerPage);
$stmt->execute();
$stmt->bind_result($id, $title, $author, $excerpt, $image_small, $image_med, $date);
while($stmt->fetch()) {
printf('<h1>%s</h1><p>%s <small> by %s on %s</small></p>',
htmlspecialchars($title),
htmlspecialchars($excerpt),
htmlspecialchars($author),
htmlspecialchars($date)
);
}
$start=1$postsPerPage=1;
$sql=“选择id、标题、作者、左侧(描述,40)作为摘录,
图像大小、图像大小、日期
发帖
按id描述订购
极限?,?“;
$stmt=$connect->prepare($sql)或die('error');
$stmt->bind_参数('ii',$start,$postsPerPage);
$stmt->execute();
$stmt->bind_result($id、$title、$author、$extract、$image_small、$image_med、$date);
而($stmt->fetch()){
printf(“%s%s由%s在%s上打印),
htmlspecialchars($title),
htmlspecialchars($摘录),
htmlspecialchars($author),
htmlspecialchars($date)
);
}
这将两个问号绑定到$start
和$postsPerPage
的整数(i
)值。不要在准备好的语句中直接使用变量,因为这会破坏准备好的语句的全部目的(除了消除解析时间)- 在SQL中使用问号作为占位符,将变量的值置于其中
- 用于将值绑定到占位符
$result->fetch()
,之后我想我会感到困惑。我应该在什么时候给这些绑定变量赋值?你不应该给它们赋值$result->fetch()
根据您的查询设置它们<显然,在调用$result->execute()
之前必须设置code>$start和$postsPerPage
。试试我的代码,它会打印出你的db内容列表啊,看看我想做的是根据我已有的变量设置我的限制。。它们来自另一个查询,该查询根据另一个表中存储的设置设置列表的数量。那么,没有办法通过该查询传递变量吗?您的问题不完整,我想您想问的是:如何从准备好的查询中检索结果。在调用$result->fetch()