php mysqli 准备好的语句
嘿嘿,我有一个快点的。有什么方法可以将变量包含到准备好的查询中吗?例如:
$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);
谢谢!
Hey, I have a quick one. Is there any way to include a variable into a prepared query? example:
$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);
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要以下内容:
这会将两个问号绑定到
$start
和$postsPerPage
的整数 (i
) 值。不要在准备好的语句中直接使用变量,因为这会破坏准备好的语句的全部目的(除了消除解析时间之外)you want the following:
this binds both question marks to integer (
i
) values of$start
and$postsPerPage
. do NOT use variables directly in prepared statements, because that would defeat the whole purpose of prepared statements (apart from eliminating parsing time)如果我没记错的话,您必须使用 bindParam 并将查询中的变量替换为问号,
您可以在 http://php.net/manual/en/pdo.prepared-statements.php
If I'm not mistaken you have to use bindParam and replace the variables in your query with a question mark
you can find more examples at http://php.net/manual/en/pdo.prepared-statements.php