Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php mysqli准备语句_Php_Mysqli_Prepared Statement - Fatal编程技术网

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中使用问号作为占位符,将变量的值置于其中
  • 用于将值绑定到占位符

嗯,看这正是我写的,同样的顺序和所有东西。变量在所有这些之前被声明,所以我不明白为什么它不显示。@tim:不,它不一样。将变量直接插入sql字符串中(不会发生转义!)。你的问题可能有误导性,我想你错过了
$result->fetch()
,之后我想我会感到困惑。我应该在什么时候给这些绑定变量赋值?你不应该给它们赋值
$result->fetch()
根据您的查询设置它们<显然,在调用
$result->execute()
之前必须设置code>$start和
$postsPerPage
。试试我的代码,它会打印出你的db内容列表啊,看看我想做的是根据我已有的变量设置我的限制。。它们来自另一个查询,该查询根据另一个表中存储的设置设置列表的数量。那么,没有办法通过该查询传递变量吗?您的问题不完整,我想您想问的是:如何从准备好的查询中检索结果。在调用
$result->fetch()